2013-10-25 44 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