ruby-on-rails - Has_many 通过关联依赖 : destroy not destroying

标签 ruby-on-rails activerecord has-many-through

我有这个模型

class XmlImport < ActiveRecord::Base
   belongs_to :video
   belongs_to :user

   has_many :events, through: :event_import_records, dependent: :destroy
   has_many :event_import_records, dependent: :destroy

   has_attached_file :xml
   validates_attachment_content_type :xml, :content_type => ["text/xml"]
end

:event_import_records 条目正在被销毁。但是 :events 不是。

  • has_many through 关联上的dependent:destroy 是否有效?
  • 还有其他写法吗?如果那不正确
  • 如何通过 event_import_records 销毁与 XmlImport 关联的所有 events

最佳答案

您可以在 Rails API 找到那:“如果与 :through 选项一起使用,连接模型上的关联必须是 belongs_to,并且被删除的记录是连接记录,而不是关联记录。”我知道它会删除连接记录,但不会删除关联记录。

如果我是你,我会尝试:

class EventImportRecord < ActiveRecord::Base
  has_many :events, dependent: :destroy
end

如果不起作用,我会交换 XmlImport 模型上的 has_many 关系的顺序,因为“请注意:依赖项是使用 Rails 的回调系统实现的,它可以工作通过按顺序处理回调。因此,在 :dependent 选项之前或之后声明的其他回调会影响它的作用。”也可以在 Rails API 的同一页找到。

关于ruby-on-rails - Has_many 通过关联依赖 : destroy not destroying,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29312068/

相关文章:

ruby-on-rails - rails 4 : counter_cache in has_many :through association with dependent: :destroy

ruby-on-rails - Rails has_many 通过条件,构建新的

ruby-on-rails - 在 rails 中使用从 URL 返回的 JSON 对象

ios - 适用于 iOS 应用程序的 Rails api?

ruby-on-rails - 如何使用 ActiveRecord 返回 Rails 中关联的模型对象

mysql - 无法使用 Rails 5.0.1 和 MySQL 更新 bool 列

ruby-on-rails - 如何在所有模型上添加 has_many 关联

ruby-on-rails - Rails 和 Globalize - 加入范围内相关模型的翻译表

ruby-on-rails - 使用:counter_cache and :touch in the same association

ruby-on-rails - 如何使用Carrierwave和Fog检查S3上是否存在镜像版本?