NoobOnRails

Ruby on Rails tips, tricks and hints for the aspiring rails hero.



Saturday, November 19, 2005

Using acts_as_taggable in Rails (quick reference)


Here are the quickfire steps to use the acts_as_taggable gem...

1. In your project database, add two tables, tags and tags_[model you're linking to], here's an example sql script:

CREATE TABLE `books` (
`id` int(6) unsigned NOT NULL auto_increment,
`title` varchar(50) NOT NULL default '',
`author` varchar(50) NOT NULL default '',
`description` text NOT NULL,
`created_on` datetime NOT NULL default '0000-00-00 00:00:00',
`updated_on` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `tags` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(30) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `tags_books` (
`tag_id` int(11) NOT NULL default '0',
`book_id` int(11) NOT NULL default '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

2. at a command prompt, from in your project directory, run

gem install acts_as_taggable

3. Add this line right above the end in your environment.rb

require_gem 'acts_as_taggable'

4. Create a tag model. You can do this manually or with the generator like so:

ruby script\generate model Tag

5. In your target object's model.rb file (books.rb in this case) add

acts_as_taggable

6. In your target object's controller (books_controller.rb for us) add this line under your
if @books.save line..

@book.tag(params[:tags])

7. Add this to your _forms partial

tags for this book: <%= text_field_tag('tags', '') %>

8. To get the items to show in yor list.rhtml view, you can call them with this

<%= book.tag_names.join(" ") %>

That's the quick way to get acts_as_taggable working. For more functionality, check this out.


22 Comments:

  • At 6:54 AM, Anonymous Anonymous said…

    Thanks for posting this. I was just getting the acts_as_taggable gem up and running when I ran across your post on the mailing list. If was very helpful. This blog is great. Keep up the great work.

     
  • At 9:47 PM, Blogger Curtis said…

    thanks...anything you want to see let me know :)

     
  • At 12:26 PM, Anonymous Anonymous said…

    for the last step, I think you have to use "@book" instead of just "book".

     
  • At 6:16 AM, Anonymous Anonymous said…

    Just used your tutorial to get the basics of tagging up and running in a development environment.

    Nice and simple. Cheers.

     
  • At 12:57 PM, Anonymous Anonymous said…

    Just a question on this. I've been trying to setup 'acts_as_taggable' and seem to running into troubles whenever using a tag twice on different instances of the same object.

    eg:
    book1.tag "horror thriller"
    book2.tag "horror vampires"

    Whenever I go to tag "book2" with a tag that has already been used I get an error outlining that there is a duplication.

    Can you confirm this?

     
  • At 8:49 AM, Anonymous Anonymous said…

    excellent article... so good i'd have your f*cking babies. I don't know how long I've been sweating over implementing tagging. cheers!

     
  • At 2:33 PM, Blogger john said…

    anyone using Rails 1.1 and wanting to be able to tag across models might want to conside the acts_as_taggable *plugin*, which is different. info here:

    http://wiki.rubyonrails.org/rails/pages/Acts+As+Taggable+Plugin

    and here:

    http://wiki.rubyonrails.org/rails/pages/ActsAsTaggablePluginHowto

     
  • At 1:03 PM, Anonymous Anonymous said…

    Thanks for this; this is really simple and sweet :-)

    can u let me know; How do i go with adding a user_id to the tags; essentially implementing user specific tags;

    I tried those howtos (user specific tags); But I couldnt make anything out of it;

     
  • At 8:28 PM, Anonymous Anonymous said…

    How do I do the reverse lookup??? finding all books tagged witha particular tag?

    mybook = Book.new..
    mybook.tag_with("RUBY RAILS AGILE")

    but how do i get the reverse?
    where given a tag I want to get all the books with that particular tag???

     
  • At 11:00 AM, Anonymous Anonymous said…

    John -- has the acts_as_taggable plugin been updated? Last I checked, the gem version was the only one under development (see here).

     
  • At 11:51 AM, Blogger Curtis said…

    msksatish you may want to look into the acts_as_Taggable gem since the plugin doesn't seem to be getting updated anymore.

     
  • At 1:28 PM, Anonymous Anonymous said…

    Shouldn't the join table be named books_tags and not tags_books?

     
  • At 2:05 PM, Blogger Curtis said…

    nope, from what i remember (haven't used this gem in a while), it didn't have to be alphabetical

     
  • At 5:36 PM, Anonymous Anonymous said…

    I've tried this tons of times, and it keeps giving me this error:

    Mysql::Error: #42S22Unknown column 'tags.article_id' in 'where clause': SELECT * FROM tags WHERE (tags.article_id = 112)

    According to this post, there shouldn't be an article_id field in the tag model.

     
  • At 6:49 PM, Blogger Mathieu said…

    you forget about the model definition

    no where you define it as acts_as_taggable

     
  • At 6:57 PM, Blogger Curtis said…

    Ah forget it, just use has_many_polymorphs:

    http://noobonrails.blogspot.com/2007/01/actsastaggable-is-no-more.html

     
  • At 6:57 PM, Blogger Mathieu said…

    sorry, forget my last comment, I just found it on your post. my bad.

    but here what's rails telling me when I try to create the Tag model.

    $ ./script/generate model -c Tag
    The name 'Tag' is reserved by Ruby on Rails.
    Please choose an alternative and run this generator again.
    $

     
  • At 7:08 PM, Blogger Curtis said…

    hmm, that may be the case. I wrote this a long time ago. I'll try it here see what I get.

     
  • At 7:10 PM, Blogger Curtis said…

    hmm

    i just did

    script/generate model tag

    and it worked for me...

     
  • At 11:12 PM, Anonymous Anonymous said…

    I have just tried as the above tutorials has displayed, i have followed each and every steps but couldnt get the answer i want, when i start to list out my all the tags it will give this error,

    You have a nil object when you didn't expect it!
    The error occurred while evaluating nil.tag_names


    i have put off all the tables and each table have two records but could not getting the cloud which i expect from this tutorials..

    Thanks in advance

    steve

     
  • At 5:55 AM, Anonymous Anonymous said…

    Dude Formating your code would be an awesome idea! but thanks none the less, great turorial

     
  • At 12:55 PM, Anonymous Anonymous said…

    For the more recent (and apparently still maintained acts_as_taggable_on_steroids) I set up a similar HOWTO.

    Actually, what I am curious about on acts_as_taggable is whether it's good manners to throw low level SQL stuff into the plug-in rather than using ActiveRecord. Anyways, my Howto on acts_as_taggable_on_steroids is there, and I go on with getting an answer to that question.

     

Post a Comment

<< Home