Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parent association for embedded document using MongoMapper

Tags:

mongomapper

If I have:

class Post
  include MongoMapper::Document

  has_many :comments
end

If I do:

class Comment
  include MongoMapper::EmbeddedDocument

  belongs_to :post # relevant part
end

Does that create an association using _root_document/_parent_document, or do I have to add the (redundant) key :post_id?

like image 795
agentofuser Avatar asked Aug 08 '26 23:08

agentofuser


1 Answers

You do not need post_id or belongs_to :post. Instead, you can use embedded_in :post. This will make a read method for _parent_reference named post so you can say comment.post instead of comment._parent_reference.

class Comment
  include MongoMapper::EmbeddedDocument

  embedded_in :post
end
like image 175
John Nunemaker Avatar answered Aug 12 '26 08:08

John Nunemaker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!