2011-08-08 213 views
2

具有Identity Map支持cascade的MongoMapper是否支持級聯刪除?它看起來並不真實,但我可能在文檔中的某處丟失了某些東西。考慮以下幾點:MongoMapper +級聯刪除?

class User 
    include MongoMapper::Document 
    many :comments 
end 

class Comment 
    include MongoMapper::Document 
    belongs_to :user 
end 

user = User.create! 
user.comments.create! 
user.destroy 

我希望user.destroy也級聯的意見 - 或者至少能夠將它配置這樣做。有任何想法嗎?

回答

0

要做到這一點,你需要使用嵌入式文件:

class User 
    include MongoMapper::Document 
    many :comments 
end 

class Comment 
    include MongoMapper::EmbeddedDocument 
    belongs_to :user 
end 

user = User.create! 
user.comments.create! 
user.destroy 

這有一些缺點太多,但...

0

你可以自己打造成爲您的User型號:

before_destroy :destroy_comments 
... 
def destroy_comments 
    comments.each {|c| c.destroy} 
end 

哪些可能被抽象/通用化...