2013-10-25 45 views
1

我有5个模型。 ServerPlatform,Game, RetentionReport,​​。我试图使用:dependent => :delete_all,但它不会工作。这是我的模特。Rails:依赖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

尝试'dependent :: destroy'。 – struthersneil

+0

我试过这个和相同的结果 – user2158382

+0

对不起,我是愚蠢的 - 它是'依赖的',而不是'依赖的'。试试:) – struthersneil

回答

5

delete_all不会触发call_backs

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

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

+0

是的,但这些解决方案都不起作用。如果我在执行“Game.destroy_all”时执行 – user2158382

+0

,那么结果是什么都不会被销毁或删除?它会毁掉所有的游戏吗? – Muntasim

+0

只是再次尝试了Game.destroy_all,它将所有'游戏'全部'平台'全部销毁'服务器'。但是如果你想在每次关联之后销毁所有使用的'dependent::destroy',它不会销毁'DataReport'或'RetentionReport' – user2158382