如何在 Rails 中生成删除级联?
我有这些模型:
class UploadFile < ActiveRecord::Base
has_many :purchases, dependent: :delete_all
end
class Purchase < ActiveRecord::Base
has_many :purchasers, dependent: :delete_all
has_many :items, dependent: :delete_all
has_many :merchants, dependent: :delete_all
has_one :upload_file
end
class Item < ActiveRecord::Base
end
class Item < ActiveRecord::Base
end
class Item < ActiveRecord::Base
end
当我删除 upload_file
时,只有 Purchase 的数据会被销毁。其余的仍然存在于数据库中。
我做错了什么?
最佳答案
你应该在这里使用 destroy_all
而不是 delete_all
。
来自api dock :
destroy_all(conditions = nil) public
Destroys the records matching conditions by instantiating each record and calling its destroy method. Each object’s callbacks are executed (including :dependent association options and before_destroy/after_destroy Observer methods).
关于mysql - Rails 依赖破坏所有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25715542/