鉴于Ryan Bates 关于 Virtual Attributes 的精彩教程,如果文章被销毁后不再使用该标签,我将如何销毁标签(而不是标签)?
我试着做这样的事情:
class Article < ActiveRecord::Base
...
after_destroy :remove_orphaned_tags
private
def remove_orphaned_tags
tags.each do |tag|
tag.destroy if tag.articles.empty?
end
end
end
...但这似乎不起作用(删除文章后标签仍然存在,即使没有其他文章使用它们)。我应该怎么做才能做到这一点?