开发者

MongoMapper + Cascade Deletes?

开发者 https://www.devze.com 2023-03-26 07:52 出处:网络
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:

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...

0

精彩评论

暂无评论...
验证码 换一张
取 消