首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >要按MongoMapper +级联删除吗?

要按MongoMapper +级联删除吗?
EN

Stack Overflow用户
提问于 2011-08-08 14:04:42
回答 2查看 203关注 0票数 2

具有身份映射的MongoMapper是否支持级联删除?它看起来不是真的,但我可能在文档中遗漏了一些东西。请考虑以下几点:

代码语言:javascript
复制
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也能级联到注释中--或者至少能够对其进行配置。有什么想法吗?

EN

回答 2

Stack Overflow用户

发布于 2011-08-16 18:24:07

为此,您需要使用嵌入式文档:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2012-08-28 02:47:26

您可以在User模型中构建自己的模型:

代码语言:javascript
复制
before_destroy :destroy_comments
...
def destroy_comments
  comments.each {|c| c.destroy}
end

可能是抽象/泛化的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6978293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档