views:

259

answers:

1

Hello all, I've recently begun playing around with MongoDB on Rails through use of the MongoMapper gem. I was wondering if there is some sort of way to have a class/object/collection be both a Document as well as an Embedded Document. I want the same entity to be both included/embedded in other documents, and also be able to exist on its own as an object.

Is this possible? Am I tackling this problem the wrong way? Any discussion/advice would be greatly appreciated as resources on the web seem low for this stuff right now.

A: 

I have a very similar usecase and the solition was to have (using your entity names):

  • a collection with People
  • a embedded model BusinessPerson that belongs_to_related :person
  • a collection with Businesses that embeds_many :busiess_people

The idea behind this was that I have extra fields in BusinessPerson that may not apply if the Person also run a different business. Let's say the role in that business, an email address or the share of it.

No you might say getting the business a person might look hard, but it isn't:

class Person
   def businesses
     Business.where('business_people._id' => self.id)
   end
end
Thomas R. Koll