这是我在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
,并且我希望每个collection_id
的Album
都将返回到NULL
,因为其父级已不存在。
如何确定删除Album
的父项Collection
时会发生这种情况?