开发者

Rails Tagging using Acts_As_Taggable_On: Finding All Tags in a Context

开发者 https://www.devze.com 2022-12-28 22:42 出处:网络
In a blog system I\'m working on, I want to use tags as a mechanism for deciding where a particular post shows up. I\'m using acts_as_taggable_on to set up two contexts, the normal :tags context and t

In a blog system I'm working on, I want to use tags as a mechanism for deciding where a particular post shows up. I'm using acts_as_taggable_on to set up two contexts, the normal :tags context and then the :audience context.

I'm using an account model to tag the post model like so:

account.tag(post, :with => "cat1, cat2", :on => :audience)

The problem I'm having is retrieving all the tags 开发者_JS百科in a particular context. I'm able to get all the tags like so:

account.owned_tags # => "cat1, cat2, tag1", where tag1 came from the normal tag context

But what I'd like to do is just get the specific tags in a context, like this:

acount.owned_tags_on :audience

Any suggestions? Thanks!


owned_tags is normal ActiveRecord association:

has_many :owned_tags, :through => :owned_taggings, :source => :tag, :uniq => true

So you can perform find with conditions on it and select tags you need:

account.owned_tags.all(:conditions => ["context = ?", "audience"])
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号