views:

444

answers:

1

Hi, I'm a little stuck with the concept of EmbeddedDocuments in MongoMapper. My Models look like this:

class Post
  include MongoMapper::Document

  many :categories
  many :qualities
end

class Category
  include MongoMapper::EmbeddedDocument

  belongs_to :post
  many :qualities
end

class Quality
  include MongoMapper::EmbeddedDocument
  key :category_id, ObjectId

  belongs_to :post
  belongs_to :category
end

My question is kind of simple: I am showing a post and want to list it's categories and all qualities belonging to it (category_id).

A: 

post.categories.each do |cat|
  puts cat
  puts cat.qualities
end

After it's depend how you save it in your database, because each EmbeddedDocument not need belongs_to with their parent. There are _parent_document adn _root_document in each document to see the belongs_to.

shingara
Actually I have a qualities array in each category, but it's empty. quality is not embedded in category, but in post.
pex
So how association qualities with categories ?
shingara
I thought two embedded documents of the same parent may have a belongs_to / many relationship as well. I save the belonging category_id inside quality.
pex