Rails在删除父级时将子关联ID设置为nil

问题描述 投票:1回答:2

这是我在CollectionAlbum模型之间建立的关联:

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,并且我希望每个collection_idAlbum都将返回到NULL,因为其父级已不存在。

如何确定删除Album的父项Collection时会发生这种情况?

ruby-on-rails activerecord associations
2个回答
1
投票

1
投票
© www.soinside.com 2019 - 2024. All rights reserved.