我有两个模型,用户和图像。
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
现在,如果您看到上面的代码,我将覆盖模型中的销毁调用。不是当用户删除他的帐户时,我想触发依赖 => 销毁回调,以便我触发 Image 的销毁。
PS:我不想使用 act_as_paranoid 或其他一些插件。我需要触发dependent=> destroy 因为我有一个非常复杂的映射,例如:
用户 has_many 图像、has_many 评论、has_many likes、has_many 活动以及更多 9 种类型的映射以及深度映射等。