Welcome to %1$s. Please log in.

Login with username, password and session length
 
News: Keep The TechnoWorldInc.com Community Clean: Read Guidelines Here.
Techno World Inc - The Best Technical Encyclopedia Online! | Forum | THE TECHNO CLUB [ TECHNOWORLDINC.COM ] | Programming Zone | Others | Using Tagging in your Ruby on Rails Application

Mark David

Using Tagging in your Ruby on Rails Application

Tagging is all the rage in the web 2.0 universe. For you aspiring Ruby on Rails developers, you are in luck, because there is a great plugin that makes adding tagging into your application a breeze. This recipe describes using the acts_as_taggable plugin on Rails 1.1

To install the plugin into your application, you will first run these commands from your project directory in your console:

script/plugin install acts_as_taggable
script/generate migration add_tag_support


The first command installs the plugin from the RubyGems repository. The second command generates a Rails Migration file under the db/migrate directory. You will want to open up that file and add in this code:

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
end


Running the next command will execute the file we just edited and create the database support for tags.

rake db:migrate

Now you need to add the following line to the top of your model class:

acts_as_taggable

And that's it. You now have tagging support on your object!

Here are some sample usage for you to try:


# tags a post with both the tags "Agile" and "Rails"
   post.tag_with("agile rails")

# returns a string containing "agile rails", space delimited.
   post.tag_list     

# find all posts tagged with Rails
   post.find_tagged_with("Rails")

Recent Updates

[August 11, 2025, 08:33:44 AM]

[August 11, 2025, 08:33:44 AM]

[August 11, 2025, 08:33:44 AM]

[August 11, 2025, 08:33:44 AM]

[May 13, 2025, 08:34:25 AM]

[May 13, 2025, 08:34:25 AM]

[May 13, 2025, 08:34:25 AM]

[April 12, 2025, 08:24:20 AM]

[April 12, 2025, 08:24:20 AM]

[April 12, 2025, 08:24:20 AM]

[April 12, 2025, 08:24:20 AM]

[March 12, 2025, 09:35:30 AM]

[March 12, 2025, 09:35:30 AM]

Subscriptions

Get Latest Tech Updates For Free!

Resources

  

Using Tagging in your Ruby on Rails Application

Started by Mark David, March 16, 2007, 08:40:40 AM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

  Print     Print       Print