gpt4 book ai didi

ruby-on-rails - MongoMapper + 级联删除?

转载 作者:太空宇宙 更新时间:2023-11-03 16:57:08 26 4
gpt4 key购买 nike

MongoMapper with Identity Map 是否支持级联删除?它似乎并不是真的,但我可能在文档中的某处遗漏了一些东西。请考虑以下事项:

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 也能级联到评论——或者至少能够配置它这样做。有什么想法吗?

最佳答案

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

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

不过这也有一些缺点......

关于ruby-on-rails - MongoMapper + 级联删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6978293/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com