Rails依赖于毁灭所有

时间:2014-09-07 23:02:54

标签: mysql ruby-on-rails ruby ruby-on-rails-4.1

如何在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时,只会销毁购买的数据。其余的仍存在于DB中。

我做错了什么?

1 个答案:

答案 0 :(得分:3)

您应该在destroy_all使用delete_all

来自api dock

  

destroy_all(conditions = nil)public

     

通过实例化每条记录来销毁记录匹配条件   并调用其destroy方法。执行每个对象的回调   (包括:依赖关联选项和   before_destroy / after_destroy Observer方法)。