# How to implement the tags using acts\_as\_taggable\_on\_steroids

**URL:** <https://www.ruby-forum.com/t/how-to-implement-the-tags-using-acts-as-taggable-on-steroids/154732>\
**Category:** Rails\
**Created:** [December 3, 2008, 10:24am UTC](https://www.ruby-forum.com/t/how-to-implement-the-tags-using-acts-as-taggable-on-steroids/154732 "2008-12-03T10:24:51Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 3, 2008, 10:24am UTC](https://www.ruby-forum.com/t/how-to-implement-the-tags-using-acts-as-taggable-on-steroids/154732/1 "2008-12-03T10:24:51Z")

</div>

Here is the my application details:

work environment:windows  
instantrails2.0

1.i have created a controller and a model for ‘aticle’ and  
‘announcements’.

2.i added acts\_as\_taggable in both the models(i.e article.rb and  
announcement.rb)

3.i created a migration for tag support like add\_tag\_support.rb which  
contains the two tables ,names ‘tags’ and ‘taggings’

class AddTagSupport \< ActiveRecord::Migration  
def self.up  
# Table for your Tags  
create\_table :tags do |t|  
t.column :name, :string  
end

```
create_table :taggings do |t|
  t.column :tag_id, :integer
  # id of tagged object
  t.column :taggable_id, :integer
  # type of object tagged
  t.column :taggable_type, :string
end

```

end

def self.down  
drop\_table :tags  
drop\_table :taggings  
end

4.i created a migration for the two table article and announcements  
for tag support like:

class AddArticlesAddAnnouncements \< ActiveRecord::Migration  
def self.up  
create\_table :articles do |t|  
t.column :name, :string  
t.column :body, :string  
t.column :created\_on, :date  
t.column :updated\_on, :date  
end  
create\_table :announcements do |t|  
t.column :body, :string  
t.column :created\_on, :date  
t.column :updated\_on, :date  
end  
end

def self.down  
drop\_table :articles  
drop\_table :announcements  
end  
end

4.then i did  
rake db:migrate

5.if i login to  
[http://localhost:3000/articles](http://localhost:3000/articles)  
or  
[http://localhost:3000/announcements](http://localhost:3000/announcements)

am able to enter the data into both tables

6 then i enter to console window

> > Artical.tag\_with(‘xxxx’)  
> > if i do the above command im getting error like method is not defined  
> > article = Article.find(1)  
> > is working  
> > article.tag\_list  
> > is giving empty array like =\> []

below are my quires:

1)can anyone help out me in this issue how to tag the tables and is  
there any changes do i need to change.  
2)i try to instal steroids like:  
ruby script/plugin install  
[http://svn.viney.net.nz/things/rails/plugins/acts\_as\_taggable\_on\_steroids](http://svn.viney.net.nz/things/rails/plugins/acts_as_taggable_on_steroids)  
its giving like no plugin found so i downloaded and just copied in to  
vendor/plugins/acts\_as\_taggable\_on\_steroids  
and started working .is it wrong?  
3)where we need to do the changes in controller and views to search  
for perticular tag from the two models for user interface?

thanks

---

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 3, 2008, 11:00am UTC](https://www.ruby-forum.com/t/how-to-implement-the-tags-using-acts-as-taggable-on-steroids/154732/2 "2008-12-03T11:00:40Z")

</div>

hi srikanth,

I just checked this against my implementation of the same plugin.  
As far as I can see, you did everything right.  
The plugin is loading, otherwise the acts\_as\_taggable in  
the model would give you an error.

Only difference:  
I used:  
@article.tag\_list = “xxx”  
instead of tag\_with, which doesn’t exist in this plugin.  
(Not sure, I think it did exist in the old acts\_as\_taggable plugin,  
so you may find it in some tutorials)

---

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 3, 2008, 11:27am UTC](https://www.ruby-forum.com/t/how-to-implement-the-tags-using-acts-as-taggable-on-steroids/154732/3 "2008-12-03T11:27:05Z")

</div>

hi Thorsten,

thanks for ur reply.

Yeah u r right.i have found that tag\_with is not working .

suppose i want to find the data from articles and announcements using  
tag how can i do that.

article table is having fields:  
name:string  
body:string

announcements table having fields:  
body:

am able to enter the data from html page

now i want to check tag function so how can i do that .  
can u help n this where i need to chane in controller and views

thanks

---

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 3, 2008, 11:39am UTC](https://www.ruby-forum.com/t/how-to-implement-the-tags-using-acts-as-taggable-on-steroids/154732/4 "2008-12-03T11:39:25Z")

</div>

We didn’t use too much of the find functions, since  
we used sphinx full text search on the database and just  
included the tags in the sphinx index.

the basic function to get articles with given tags:

@article = Article.find\_tagged\_with(“something”)

This takes a few options:  
Pass either a tag, string, or an array of strings or tags.  
:exclude - Find models that are not tagged with the given tags  
:match\_all - Find models that match all of the given tags, not just  
one  
:conditions - A piece of SQL conditions to add to the query

---

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 3, 2008, 12:28pm UTC](https://www.ruby-forum.com/t/how-to-implement-the-tags-using-acts-as-taggable-on-steroids/154732/5 "2008-12-03T12:28:10Z")

</div>

On the first view you will need a form to enter the search like:

\<% form\_tag(search\_articles\_path(), :method =\> :get) do %\>  
\<%= label(:search, :tag, “Search:”) %\>  
  
  
  
\<% end %\>

this would give the search in params[:search]

depending on some details, you can use this directly like  
@articles = Article.find\_tagged\_with(params[:search])

But there are many ways to do this and much depends on details,  
what exactly to search and display.  
Some Ajax response may be another option.  
But can’t help you there, since that would require more knowledge  
about the needs and actual state of your site.

---

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 3, 2008, 12:46pm UTC](https://www.ruby-forum.com/t/how-to-implement-the-tags-using-acts-as-taggable-on-steroids/154732/6 "2008-12-03T12:46:57Z")

</div>

hi,

from the above code,  
what i understud is  
search is a method and it shold be defined in the controller right like  
def search  
@articles = Article.find\_tagged\_with(params[:search])  
end  
or how it will be exactly can u help me regarding this

thanks  
.  
and  
to enter tag as search parameter on first form we should have some  
text box to enter right.how to do it.

---

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 3, 2008, 11:54am UTC](https://www.ruby-forum.com/t/how-to-implement-the-tags-using-acts-as-taggable-on-steroids/154732/7 "2008-12-03T11:54:04Z")

</div>

hi,

that commond is working on console,but i want give the tag name from  
html and it should take the tag paramaer and able to list out the tags  
which r linking to article and announcements.For this w need to update  
the controller of article and announcements and at the same time we  
need to update the views like  
index.html.erb,show.html.erb,new.html.erb,edit.html.erb files .

so here im getting stuck :  
1)how to give the tag input from the html file (it works like search  
for all the taggables items from database)  
2)where exatly i need to change the controller and views

if u have any idea any sample application let me know

thanks

---

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 3, 2008, 1:39pm UTC](https://www.ruby-forum.com/t/how-to-implement-the-tags-using-acts-as-taggable-on-steroids/154732/8 "2008-12-03T13:39:50Z")

</div>

hi ,

sorry for disturbing u .

can u explain wat that code u have given will do,  
i added that coode in index.html.erb  
but im gettin unintialized method error:it is expected because the  
method i did not added in controller so can u give dea where can i  
modify the code  
search\_articles\_path(),

thanks

---

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 3, 2008, 1:56pm UTC](https://www.ruby-forum.com/t/how-to-implement-the-tags-using-acts-as-taggable-on-steroids/154732/9 "2008-12-03T13:56:17Z")

</div>

hi thanks for that

in code snippet :  
\<% form\_tag(search\_articles\_path(), :method =\> :get) do %\>  
\<%= label(:search, :tag, “Search:”) %\>  
  
  
  
\<% end %

my quieries:  
1):method =\> :get  
the above method shoud be defined in controller right.  
2)above code shoud take tag parameter right so from where it is  
getting the tag parameter to search the database?

can u clarify this.

thank u very much

---

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 3, 2008, 1:50pm UTC](https://www.ruby-forum.com/t/how-to-implement-the-tags-using-acts-as-taggable-on-steroids/154732/10 "2008-12-03T13:50:24Z")

</div>

search\_articles\_path would be generated by RESTful routing like (in  
routes.rb):

map.resources :articles, :colection =\> [:search]

but you can use any other path helper instead or a string with the url

---

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 3, 2008, 2:00pm UTC](https://www.ruby-forum.com/t/how-to-implement-the-tags-using-acts-as-taggable-on-steroids/154732/11 "2008-12-03T14:00:59Z")

</div>

> 1):method =\> :get  
> the above method shoud be defined in controller right.

no, the :method =\> :get relates to form methods in html, nothing to do  
with methods in Rails  
if you defined the routes like  
map.resources :articles, :collection =\> [:search]  
and use the path helper search\_articles\_path, then the method must be  
in  
controler: Articles  
method: search

> 2)above code shoud take tag parameter right so from where it is  
> getting the tag parameter to search the database?

the text entered by the user in the form will be in  
params[:search]

---

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 3, 2008, 2:22pm UTC](https://www.ruby-forum.com/t/how-to-implement-the-tags-using-acts-as-taggable-on-steroids/154732/12 "2008-12-03T14:22:08Z")

</div>

hi,

You mean to say i need to add

1)map.resources :articles, :collection =\> [:search] in route.rb

2)do i need to add method in controller like

## def get

end

3)Can we give the tag parameter as text box entry from the  
forms(index.html.erb) and that tag parameter can be searched in the  
database?

can u clear on this

---

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 3, 2008, 2:38pm UTC](https://www.ruby-forum.com/t/how-to-implement-the-tags-using-acts-as-taggable-on-steroids/154732/13 "2008-12-03T14:38:36Z")

</div>

> You mean to say i need to add  
> 1)map.resources :articles, :collection =\> [:search] in route.rb

yes

> 2)do i need to add method in controller like
> 
> ## def get
> 
> end

no, it should be:  
def search  
…  
end

the :collection =\> [:search]  
declares the existence of this search method in the articles  
controller

> 3)Can we give the tag parameter as text box entry from the  
> forms(index.html.erb) and that tag parameter can be searched in the  
> database?

yes, if you have a textbox named “search”, then  
params[:search]  
will have the text entered in this box. You  
can give it any other name of course, params will have the value  
with the same name.  
(You can see what params are returned in the development.log)

To use this for searching:  
@articles = Article.find\_tagged\_with(params[:search])

then you should have all artcles with that tag in @articles  
and can display them in the view

---

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 4, 2008, 6:16am UTC](https://www.ruby-forum.com/t/how-to-implement-the-tags-using-acts-as-taggable-on-steroids/154732/14 "2008-12-04T06:16:26Z")

</div>

Hi Thorsten,

really very helpful ur ideas to me as i am very new to rails and sql.

I HAVE UPDATED MY APPLICATION LIKE BELOW as per ur suggetions:

1)I added this search :map.resources :articles, :collection =\>  
[:search] in route.rb like below

map.connect ‘:controller/:action/:id’  
map.connect ‘:controller/:action/:id.:format’  
#added for search  
map.resources :articles, :collection =\> [:search]  
end  
2)i defined a search method in article controller like below:

def search  
@articles = Article.find\_tagged\_with(params[:search])  
end

3)i added the below code in idex.html.erb as u said and added a  
commond to get a search box for tag entry.

Search:

\<% form\_tag(search\_articles\_path(), :method =\> :get) do %\>  
\<%= label(:search, :tag, “Search:”) %\>  
  
  
  
\<% end %\>

then i trid [http://localhost:3000/articles](http://localhost:3000/articles)

i am able to see on index page like below list:

Listing articles  
Name Body  
Cricket india vs pak tour Show Edit Destroy  
politics HM quits Show Edit Destroy

New article

Search:

from the index page i am able to do show,edit,destory and new article  
functionalities and i am getting the search box to enter a tag name.

then my quiries:  
1)if i enter some tag name then i press the search button i m getting  
erors like:

ActiveRecord::RecordNotFound in ArticlesController#show  
Couldn’t find Article with ID=search  
RAILS\_ROOT: E:/srikanth/InstantRails-2.0-win/rails\_apps/blog  
RequestParameters:  
{“search”=\>“fun”,  
“x”=\>“48”,  
“y”=\>“18”,  
“id”=\>“search”}

2)if the search isable to do searching then the reasult shoud be list  
out on index rt.so for this do we need update in any one of views(to  
get the results) can u tell me which view i ned to modify and how ?

3)small doubt:

i have a table like contacts with columns like:

id name age location  
1 a 12 ap  
2 b 13 ap  
3 c 12 up

corresponding tag table like:  
id name  
1 xx  
2 yy  
3 zz

here my douts are  
1)the tag name shoud be one of the parameter like name or age or place  
or it should be the entry name of perticuler column?  
here actaully i given tag name as fun and serious as tag names but we  
did not mention any relation table contents of article ,even if i do  
article.find\_taggable\_with(‘fun’) it is giving the details of teble  
entrys.i did not understand the logic here can u help in this?

thanks  
Srikanth  
2)suppose i want to find all people from place ap i want to search  
,how can i do that?  
3)how the taging table is updating with all the above columns in the  
tagging table?

tagging table like:  
id tid taggable\_id taggable\_type

---

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 4, 2008, 10:29am UTC](https://www.ruby-forum.com/t/how-to-implement-the-tags-using-acts-as-taggable-on-steroids/154732/15 "2008-12-04T10:29:42Z")

</div>

hi thorsten,  
actually i got stuck here itself i am waiting for ur reply if you  
could do that its very help ful to me

thanks

---

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 4, 2008, 7:56am UTC](https://www.ruby-forum.com/t/how-to-implement-the-tags-using-acts-as-taggable-on-steroids/154732/16 "2008-12-04T07:56:06Z")

</div>

hi thorsten

on console i have tried below commonds:

> > Article.find(1)  
> > working fine  
> > article.tag\_list  
> > giving error like unfdefined local variable or method article  
> > But if i check the tas table from mysql prompt i am able to see the  
> > tag names like xx,yy…

i tried to give the tag names like

> > article.tag\_list = "fun serious "  
> > here also giving the same error but i trie yester in the same way it  
> > showned like  
> > [“fun”,“serious”]

can u tell me how togive the tag name on console without using  
article.tag\_with method because this method not working.

thanks

---

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 4, 2008, 2:08pm UTC](https://www.ruby-forum.com/t/how-to-implement-the-tags-using-acts-as-taggable-on-steroids/154732/17 "2008-12-04T14:08:45Z")

</div>

On 4 Dec 2008, at 06:55, sreekanth.G wrote:

> hi thorsten
> 
> on console i have tried below commonds:
> 
> > > Article.find(1)  
> > > working fine  
> > > article.tag\_list  
> > > giving error like unfdefined local variable or method article

Well yes - you haven’t defined article.  
if you do article = Article.find(1)  
then you should be able to play around with article.tag\_list etc…

Fred

---

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 4, 2008, 2:26pm UTC](https://www.ruby-forum.com/t/how-to-implement-the-tags-using-acts-as-taggable-on-steroids/154732/18 "2008-12-04T14:26:23Z")

</div>

On 4 Dec 2008, at 13:10, sreekanth.G wrote:

> Hi
> 
> i am clear abot that but  
> if i write below
> 
> > > article\_tag\_with(‘tagname’)  
> > > undefined methos article  
> > > how can i do this?  
> > > thanks

Well that’s the same thing. you’ve got to set article to be an  
instance of article before you can call article.tag\_with ‘foo’

Fred

---

<div class="post-metadata">

**Author:** ![srikanth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@srikanth](https://www.ruby-forum.com/u/srikanth)\
**Post date:** [December 4, 2008, 2:11pm UTC](https://www.ruby-forum.com/t/how-to-implement-the-tags-using-acts-as-taggable-on-steroids/154732/19 "2008-12-04T14:11:07Z")

</div>

Hi

i am clear abot that but  
if i write below

> > article\_tag\_with(‘tagname’)  
> > undefined methos article  
> > how can i do this?  
> > thanks

On Thu, Dec 4, 2008 at 6:37 PM, Frederick C.
