views:

35

answers:

1

Hi,

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

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

A: 

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
yayitswei