Follow Along as I Stumble on the Path to Learning RoR

Adding Tagging to Rails App

I wanted to add tagging to some models, so I followed Ryan Bates great Railscast http://railscasts.com/episodes/382-tagging.  In it he shows how to do it with your own code as well as with the gem acts-as-taggable-on.  I decided to use the acts-as-taggable-on because it has a little more functionality including the ability to work with context – for me, that means having multiple tags per model.

There’s a couple of things I needed to figure out about acts-as-taggable-on that I needed to figure out myself.

  1. using context.  to have multiple tags, you can add a line like this in your model:
    acts_as_taggable_on :skills, :interests

    this will allow you to record tags on a model (let’s say users) for skills and interests

  2. i needed my context tags to be able to associated with my user.  my tags were acts_as_taggable_on were on another model – recipes, such as:

acts_as_taggable_on :ingredients, :flavors (in recipe.rb)

I tried to get the user tags associated to the user with

acts_as_tagger

in user.rb, but that didn’t work.  So I ended up using this:

@user.tag(@receipe,  :on => :ingredients, :with => :ingredients)

which worked (populated tagger_id and tagger_type) in the Taggings table.

Advertisements

Comments are closed.