mysql - Rails 依赖破坏所有

标签 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 时,只有 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/

相关文章:

mysql - 找出日期之间的差异

MySQL 查询优化 - 子查询和使用 where;使用临时的;使用文件排序

php - PphpMyAdmin - 错误无法在没有错误的情况下启动 session : How to fix?

ruby-on-rails - 如何利用 "username or Email"登录设备?

ruby-on-rails - 将 Rails 应用程序更改为生产环境

数组的 Ruby 多个 group_by

php - 使用 PHP 在第 7 天使 Cookie 过期

ruby-on-rails - 大型机器上的 Rails 应用程序仅获得 60 个请求/秒的基准测试结果

c++ - 无法创建 C++ ruby​​ 扩展

objective-c - CocoaPods 没有这样的文件或目录