我有5个型号。 Server
,Platform
,Game
,RetentionReport
,DataReport
。我正在尝试使用: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
答案 0 :(得分:5)
delete_all
不会触发call_backs
。
如果你有Game.destroy_all
它会做你想要的。
您可以在关联声明中使用:dependent => :destroy
或:dependent => :delete_all
。前者将在关联中运行回调,而后者则不会。