开发者

Suggestions on how to track tag count for a particular object

开发者 https://www.devze.com 2023-01-02 17:00 出处:网络
I\'m looking for suggestions on how to track the number of tags associated with a particular object in Rails. I\'m using acts_as_taggable_on and it\'s working fine. What I would like to be able to do

I'm looking for suggestions on how to track the number of tags associated with a particular object in Rails. I'm using acts_as_taggable_on and it's working fine. What I would like to be able to do is search for all objects that have no tags, preferably through a scope i.e. Object.untagged.all

开发者_如何学Python

My first thought was to use an after_save callback to update an attribute called "taggings_count" in my model:

def update_taggings_count
  self.taggings_count = self.tag_list.size
  self.save
end

Unfortunately, this does the obvious thing of putting me in an infinite loop. I need to use an after_save callback because the tag_list is not updated until the main object is saved.

Would appreciate any suggestions as I'm on the verge of rolling my own tagging system.

Regards

Robin


I did the same thing, but put the function in before_save, like so

scope :untagged, where("taggings_count = 0")
before_save :update_taggings_count

def update_taggings_count
  self.taggings_count = tag_list.size
end
0

精彩评论

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