开发者

MongoDB Embedded Documents / Documents with Rails

开发者_Python百科

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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜