如何触发:d ependent=>销毁



>我有两个模型,User和Image。

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或其他一些插件。我需要触发 dependent=> 销毁,因为我有一个非常深的复杂映射,如下所示:

用户has_many图像, has_many评论, has_many喜欢, has_many活动 以及更多 9 种类型的映射和深度映射等。

你只需要模拟依赖销毁。

在您的销毁方法

中调用映像的所有销毁方法

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
    self.images.each(&:destroy)
  end 
end 

也许为了让事情变得更容易一些,您可以将归档模型和可归档模型的行为封装在模块中:

module Archivable
  def archive!
    self.archive = 1
    self.deleted_at = Time.now
    self.save
  end
end
module Archiver
  def destroy
    self.archive = 1
    self.deleted_at = Time.now
    archive_models!
    self.save
    freeze
  end
  def archive_models!
    archivable_models.each do |model|
      model_name = model.name.pluralize.downcase.to_s
      self.send(model_name).each { |m| m.archive! }
    end
  end
  def archivable_models
    ActiveRecord::Base.send(:subclasses).select { |m| m.method_defined?(:archive!) }
  end
end
class Image < ActiveRecord::Base
  include Archivable
  belongs_to :user, :counter_cache => true
end
class User < ActiveRecord::Base
  include Archiver 
  has_many :images,:order => "created_at DESC", :conditions => "archive = 0", :dependent => :destroy
  attr_accessible :name
end

这是一个非常黑客式的实现,但它有效。如果您按照帖子中所述添加注释模型,则只需将 has_many 语句添加到用户,并在评论模型中包含可存档:

class User < ActiveRecord::Base
  include Archiver 
  has_many :images,:order => "created_at DESC", :conditions => "archive = 0", :dependent => :destroy
  has_many :comments,:order => "created_at DESC", :conditions => "archive = 0", :dependent => :destroy
  attr_accessible :name
end

class Comment < ActiveRecord::Base
  include Archivable
  belongs_to :user, :counter_cache => true
end

相关内容

  • 没有找到相关文章

最新更新