关于如何跟踪特定对象的标签计数的建议
我正在寻找有关如何跟踪与 Rails 中特定对象关联的标签数量的建议。我正在使用 acts_as_taggable_on 并且工作正常。我希望能够做的是搜索所有没有标签的对象,最好是通过范围即 Object.untagged.all
我的第一个想法是使用 after_save 回调来更新模型中名为“taggings_count”的属性:
def update_taggings_count
self.taggings_count = self.tag_list.size
self.save
end
不幸的是,这显然让我陷入了无限循环。我需要使用 after_save 回调,因为在保存主对象之前 tag_list 不会更新。
我将不胜感激任何建议,因为我即将推出自己的标签系统。
问候
罗宾
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我做了同样的事情,但是把函数放在 before_save 中,就像这样
I did the same thing, but put the function in before_save, like so