我有两个模型,User和Image。我该如何触发:dependent => destroy
class User < ActiveRecord::Base
has_many :images,:order => "created_at DESC", :dependent => :destroy, :conditions => "archive = 0"
def destroy
self.archive = 1
self.deleted_at = Time.now
self.save
end
end
class Image < ActiveRecord::Base
belongs_to :user , :counter_cache => true
def destroy
self.archive = 1
self.deleted_at = Time.now
self.save
end
end
现在,如果你看到上面的代码,我重写模型中的销毁呼叫。不是当用户删除他的帐户时,我想触发依赖=>销毁回调,以便我触发摧毁图像。
PS:我不想使用act_as_paranoid或其他插件。我需要触发依赖=>破坏,因为我有一个非常深的复杂的映射是这样的:
用户的has_many图像, 的has_many评论, 的has_many喜欢, 的has_many活动 和一些9种类型的映射,并再次深马平等等。