【发布时间】:2018-05-23 07:44:18
【问题描述】:
我有三个模型。一个collection 有许多searches 到items。
当 collection 被销毁时,我希望它的 items 被销毁,但 searches 被取消,因为它们本身仍然是有效的对象。
这个可以吗?
这是我的模型:
class Collection < ApplicationRecord
has_many :searches, through: :items
has_many :items
has_many :searches, through: :suggestions
has_many :suggestions
end
class Item < ApplicationRecord
belongs_to :collection
belongs_to :search
end
class Search < ApplicationRecord
has_many :collections, through: :items
has_many :items
has_many :collections, through: :suggestions
has_many :suggestions
end
【问题讨论】:
-
你可以写一个方法来做到这一点。会是明确的
-
谢谢!我确信有办法在
has_many :items上调用dependent: destroy和在has_many :searches, through :items上调用dependent :nullify,但我想没有? -
顺便说一句,您需要重命名
has_many :searches之一。模型不能有两个具有相同名称的不同关联
标签: ruby-on-rails activerecord rails-activerecord polymorphic-associations