How to reference an embedded document in Mongoid?

Because Maps are their own collection, you would need to iterate over every Map collection searching within for the Location your Player is referenced.

Because Maps are their own collection, you would need to iterate over every Map collection searching within for the Location your Player is referenced. You can't access embedded documents directly. You have to enter through the collection and work your way down.To avoid iterating all of the Maps you can store both the Location reference AND the Map reference in your Player document.

This allows you to chain criteria that selects your Map and then the Location within it. You have to code a method on your Player class to handle this. Def location self.map.locations.

Find(self. Location_id) end So, similar to how you answered yourself except you could still store the location_id in your player document instead of using the coord attribs. Another way would be to put Maps, Locations, and Players in their own collections instead of embedding the Location in your Map collection.

Then you could use reference relationships without doing anything fancy... however your really just using a hierarchical database likes it's a relational database at this point...

PLEASE go and VOTE for the "virtual collections" feature on MongoDB's issue tracker: jira.mongodb.org/browse/SERVER-142 It's the 2nd most requested feature, but it still hasn't been scheduled for release. Maybe if enough people vote for it and move it to number one, the MongoDB team will finally implement it.

In my use case, there is no need for the outside object to reference the embedded document. From the mongoid user group, I found the solution: Use referenced_in on the embedded document and NO reference on the outside document. Class Page include Mongoid::Document field :title embeds_many :page_objects end class PageObject include Mongoid::Document field :type embedded_in :page, :inverse_of => :page_objects referenced_in :sprite end class Sprite include Mongoid::Document field :path, :default => "/images/something.

Png" end header_sprite = Sprite. Create(:path => "/images/header. Png") picture_sprte = Sprite.

Create(:path => "/images/picture. Png") p = Page. Create(:title => "Home") p.

Page_objects. Create(:type => "header", :sprite => header_sprite) p. Page_objects.first.

Sprite == header_sprite.

Oh woa... this actually works like a charm! Not sure it was intended this way but that's really cool. Thx for the tip.

– Alex Mar 25 at 8:01 2 This is perfectly doable because you're referencing a top level collection from within an embedded collection. What the OP wants to do is "reference an embedded collection from outside of it's parent collection", which is an entirely different thing. For example if you needed to access an embedded PageObject from your Sprite... you'd have to go through the parent Page collection to find it.

Or like in my response you can store both a Page and a PageObject reference in your Sprite so you don't have to iterate over the entire Page collection. – Dave Rapin May 2 at 16:58.

My current workaround is to do the following: class Map include Mongoid::Document embeds_many :locations references_many :players, :inverse_of => :map end class Player referenced_in :map field :x_coord field :y_coord def location=(loc) loc.map. Users self. X_coord).

And(:y_coord => self. Y_coord). First end end This works, but feels like a kluge.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions