MongoMapper + Cascade Deletes?
Does MongoMapper with Identity Map support cascade deletes? It doesn't really seem to, but I could be missing something somewhere in documentation. Consider the following:
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
I would expect user开发者_开发问答.destroy
to also cascade to comments -- or at least be able to configure it to do so. Any ideas?
To do this, you need to use embedded documents:
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
This has some cons too though ...
You can build your own into your User
model:
before_destroy :destroy_comments
...
def destroy_comments
comments.each {|c| c.destroy}
end
Which could possibly be abstracted/genericized...
精彩评论