ruby-on-rails-3acts-as-taggable-onacts-as-taggable

How to search by context using acts-as-taggable-on


I'm having trouble understanding how the "tagged_with" method would work in the case where I wanted to search "by context".

For example, if I have the following @user:

class User < ActiveRecord::Base
      acts_as_taggable_on :skills, :interests
    end

@user1 = User.new(:name => "Bobby")
@user1.interest_list = "1, 2"     
@user1.skill_list = "5, 3, 4"
@user1.save

@user2 = User.new(:name => "Al")
@user2.interest_list = "2, 4"     
@user2.skill_list = "1, 3, 4"
@user2.save

I know that

User.tagged_with(["1"], :any => true)

will give me back both @user1 and @user2, b/c it doesn't distinguish between interest"1" tag or skill"1" tag. But my question is: Is there a way to search for Users with tag "1" in "interests" context only and not "skills" context ? In other words, how do i fetch just @user2 by specifying "skill" "1" somehow ?

Thanks for you help, guys


Solution

  • Use the on option:

    User.tagged_with(["1"], :on => :skills)
    

    See the section entitled "Dynamic Tag Contexts" at the readme and this cheat sheet for a reference.