Rails:Dependent delete_all无法正常工作

时间:2013-10-25 22:57:39

标签: ruby-on-rails ruby ruby-on-rails-3 activerecord

我有5个型号。 ServerPlatformGameRetentionReportDataReport。我正在尝试使用:dependent => :delete_all,但它无法正常工作。这是我的模特。

class Game < ActiveRecord::Base
  attr_accessible :name

  has_many :platforms, :dependent => :delete_all
end

class Platform < ActiveRecord::Base
  attr_accessible :name, :game_id, :company_id

  belongs_to :game
  has_many :servers, :dependent => :delete_all
end

class Server < ActiveRecord::Base
  attr_accessible :name, :region, :device_type, :platform_id, :platform_server_id

  belongs_to :platform
  has_many :gm_data_reports, :dependent => :delete_all
  has_many :gm_retention_reports, :dependent => :delete_all

  delegate :company_id, :to => :platform

  validates :platform_server_id, :uniqueness => {:scope => :platform_id}
end

class DataReport < ActiveRecord::Base

 belongs_to :server
end

class RetentionReport < ActiveRecord::Base

 belongs_to :server
end

每当我在终端中运行Game.delete_all时,即使Platforms

,也不会删除任何内容

1 个答案:

答案 0 :(得分:5)

delete_all不会触发call_backs

如果你有Game.destroy_all它会做你想要的。

您可以在关联声明中使用:dependent => :destroy:dependent => :delete_all。前者将在关联中运行回调,而后者则不会。