2013-10-29 95 views
3

我的問題很簡單,只要是這個線程 的頭銜,我有兩個類依賴:破壞無法正常工作(的has_many協會)

class Category < ActiveRecord::Base 

    has_many :subcategories, dependent: :destroy 

    accepts_nested_attributes_for :subcategories 

end 

class Subcategory < ActiveRecord::Base 
    belongs_to :category 
end 

,但是當我打link_to "Delete", category_path(category), method: :delete請求類別被摧毀,但所有子類別都沒有發生任何事情。

編輯

服務器日誌:

Started DELETE "/categories/8" for 127.0.0.1 at 2013-10-29 01:38:01 +0100 
Processing by CategoriesController#destroy as HTML 
    Parameters: {"authenticity_token"=>"rOTo3ROv/QsQRMPyCljbMvrViKgihC1CowBdlHdq7O4=", "id"=>"8"} 
    Category Load (0.3ms) SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 8 LIMIT 1 
    SQL (0.5ms) DELETE FROM `categories` WHERE `categories`.`id` = 8 
Redirected to http://localhost:3000/categories 
Completed 302 Found in 3ms (ActiveRecord: 0.8ms) 


Started GET "/categories" for 127.0.0.1 at 2013-10-29 01:38:01 +0100 
Processing by CategoriesController#index as HTML 
    Category Load (0.3ms) SELECT `categories`.* FROM `categories` 
    Rendered categories/index.html.haml within layouts/application (0.9ms) 
    Rendered application/_header.html.haml (0.1ms) 
Completed 200 OK in 6ms (Views: 5.5ms | ActiveRecord: 0.3ms) 
+0

你可以發佈你的應用服務器日誌嗎?它是什麼顯示,當你刪除一個類別? –

+0

我們需要查看您要調用的控制器代碼以刪除類別。 – sevenseacat

回答

13

你打電話的類別delete?您應該撥打destroy

+0

這可能是原因。 'delete'會跳過所有與ActiveRecord相關的回調(包括依賴管理),而'destroy'則不會。 – sevenseacat

+1

似乎是新手常犯的一個錯誤 –