之前,当我试图从模型中删除一条记录时,我得到了一个外键约束错误,所以我在关系中添加了dependent: :destroy
选项。现在的关系是这样的:
class Package < ActiveRecord::Base
has_one :package_event, dependent: :destroy
has_one :tag_package, dependent: :destroy
has_one :current_bin, :through => :tag_package
has_many :tag_packages, lambda{ with_deleted }, dependent: :destroy
has_one :tag, :through => :tag_package
has_one :tag_type, :through => :tag
has_one :tag_issuer, :through => :tag
has_many :scans, lambda{ distinct }, :through => :tag
但是现在当我试图从模型中销毁一条记录时,我得到了一个ActiveRecord::RecordNotDestroyed,它没有显示错误消息,所以我不确定这个错误背后的原因是什么。
>> Package.first.destroy!
=> ActiveRecord::RecordNotDestroyed (Failed to destroy the record)
我怀疑这与正在发生的has_many_through
关系有关。有人知道如何解决这个问题吗?
1条答案
按热度按时间7y4bm7vi1#
您可以修复错误(修复ActiveRecord::RecordNotDestroyed =〉error),然后查看error.record.errors。
我从这个答案中找到了解决办法:https://stackoverflow.com/a/53872915/4785305
这可能是因为您有一个before_destroy回调函数,阻止销毁某个关联的记录。