2012-04-24 142 views
1

如果有:embeds_many :foos依賴=>:需要embeds_many來銷燬?

:dependent => :destroy必填?或者,如果我銷燬父元素。各自的foos將被自動刪除?

我試圖檢查控制檯Foo,但得到:

Mongoid::Errors::InvalidCollection: Access to the collection for Foo is not allowed since it is an embedded document, please access a collection from the root document. 

回答

3

嵌入式文檔不能直接訪問。它們包含在父文檔中,只能通過父文檔訪問。當你銷燬一個父文檔時,嵌入的文檔會隨着它一起被刪除,但是如果你有任何特定的回調來銷燬嵌入式文檔,你應該看看cascade_callbaks

+0

看看這個:http://mongoid.org/en/mongoid/v3/relations.html。他使用dependent:has_many:albums,dependent :: delete,belongs_to:label,dependent :: nullify。如果它自動銷燬嵌入式文檔,那麼爲什麼有一個相關的刪除和無效?沒有什麼可以抵消的。 – Donato 2015-03-07 04:53:54

+0

由於has_many和belongs_to不是嵌入關係,它們是僅存儲屬於邊的關鍵的參照關係。 – rubish 2015-05-21 21:58:14