如果我有:
class Post
include MongoMapper::Document
has_many :comments
end
Run Code Online (Sandbox Code Playgroud)
如果我做:
class Comment
include MongoMapper::EmbeddedDocument
belongs_to :post # relevant part
end
Run Code Online (Sandbox Code Playgroud)
这是使用_root_document/ 创建关联_parent_document,还是必须添加(冗余)key :post_id?
小智 9
你不需要post_id或belongs_to:post.相反,您可以使用embedded_in:post.这将为_parent_reference命名post的read方法,因此你可以说comment.post而不是comment._parent_reference.
class Comment
include MongoMapper::EmbeddedDocument
embedded_in :post
end
Run Code Online (Sandbox Code Playgroud)