你
可以
只需删除项目,只需添加
dependent: :destroy
到
has_many :items
.
class Collection < ApplicationRecord
has_many :searches, through: :items
has_many :items, dependent: :destroy
end
销毁集合后,该项将被销毁,但搜索将保留。
第二个解决方案
你甚至可以申请
dependent: :nullify
到
has_many :searches
-得到同样的结果。
class Collection < ApplicationRecord
has_many :searches, through: :items, dependent: :nullify
has_many :items
end
从
the docs
:
collection.delete(对象,_)
通过将其外键设置为,从集合中删除一个或多个对象
NULL
. 如果对象与
从属::销毁
,如果它们与
dependent: :delete_all
.
如果
:through
使用选项,则默认情况下会删除联接记录(而不是使其无效),但可以指定
从属::销毁
或
从属::无效
以覆盖此。