鑑於Ryan Bates's great tutorial on Virtual Attributes,如果一旦文章被銷燬,標籤不再被使用,我將如何去銷燬標籤(而不是標籤)?RoR:破壞與has_many,:通過孤立的關聯
我試圖做這樣的事情:
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
...但是,這似乎並沒有工作(文章被刪除後仍然存在的標籤,即使沒有其他物品使用它們)。我應該怎麼做才能做到這一點?
謝謝;我想我假設'標籤'是'self.tags',這可能不會起作用(哦,睡眠剝奪......)。 – neezer 2009-11-16 17:01:20