【发布时间】:2019-12-01 14:50:41
【问题描述】:
这是我在Collection 和Album 模型之间建立的关联:
class Collection < ActiveRecord::Base
has_many :children, class_name: "Collection", foreign_key: "parent_id"
belongs_to :parent, class_name: "Collection", foreign_key: "parent_id", optional: true
has_many :albums
end
class Album < ActiveRecord::Base
has_many :photos, dependent: :destroy
belongs_to :collection, optional: true
end
我刚刚删除了所有Collection,我希望每个Album 的collection_id 都返回到NULL,因为父级不再存在。
我如何确保在删除 Album 的父 Collection 时发生这种情况?
【问题讨论】:
标签: ruby-on-rails activerecord associations