Does the :any flag overwrites the :on (context) flag in acts_as_taggable_on's tagged_with?

僤鯓⒐⒋嵵緔 提交于 2019-12-12 04:15:42

问题


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'



来源:https://stackoverflow.com/questions/5539583/does-the-any-flag-overwrites-the-on-context-flag-in-acts-as-taggable-ons-ta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!