Does the :any flag overwrites the :on (context) fl

2019-09-10 09:25发布

问题:

I'm confused with how the flags :on and :any works together in the "tagged_with" method of acts_as_taggable_on.

For example, if I have the following users @user1 and @user2:

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

@user1 = User.new(:name => "Bobby")
@user1.interest_list = "1, 15"     
@user1.skill_list = "2, 17"
@user1.save

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

When I want to get all users who's interest-list include any of the tag ["2", "50"], i tried this:

User.tagged_with(["2", "50"], :on => :interests, :any => true)

The problem is I get back @user1 (which has "2" in the skill_list, not interest_list), even though I was expecting none. It seems that the flag :any might have overwritten the flag :on. Is there a way to actually perform the filter I described above?

Also, a side question is, how do you find all the flags available to a method? For example, tagged_with has :on, :any, :match_all, ..., how do i list all of them ?

Thanks for your help, everyone!

回答1:

how do you find all the flags available to a method?

Try to develop your "source code reading" skills. A good 3rd party library will always document the available options for the key methods that it expose.



回答2:

All right, I found the where the problem is. Thanks to aurelian for pointing me to the source of acts_as_taggable_on. It turned out that the fix for :any overwriting :on issue was fixed as of March 18 on github master. And the gem from rubygem.org doesn't include this fix yet.

So instead of building my app from rubygem.org using: gem 'acts_as_taggable_on'

I just point it straight to the git source:
gem 'acts_as_taggable_on', :git => 'git://github.com/mbleigh/acts-as-taggable-on.git'