-
Type: Improvement
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: None
Cross Posted from StackOverflow.
I'm likely missing something here, but it seems like this is a performance oversight. I first noticed this when looking at the query logs of Errbit and noticing hundreds of queries for the same objects.
It seems that all the children of a has_many relation don't have a reference back to their parent object after being loaded through the relation. i.e. accessing parent.children.map &:parent will get the parent from the DB _once for each child_ instead of being setup with the in-memory copy of parent
Example:
Using a very simple belongs_to / has_many setup:
class Person include Mongoid::Document field :name has_many :posts, :inverse_of => :person end class Post include Mongoid::Document field :text belongs_to :person, :inverse_of => :posts end
Then, in the Rails console, a simple demonstration:
Loading development environment (Rails 3.2.12)
[1] pry(main)> tom = Person.create(:name => 'Tom')
[2] pry(main)> tom.posts.create(:text => 'stuff')
[3] pry(main)> tom.posts.create(:text => 'other stuff')
[4] pry(main)> Person.first.posts.map
=> [50687740, 50719060]
Note that last line, each person reference points to a different ruby object. I'm using ruby's object_id attribute to highlight the fact that these are literally different objects and this requires two round-trips to the database.
Why isn't the parent relation just a reference to the parent object after loading through the has_many relation?
- is duplicated by
-
MONGOID-4436 An instance of Mongoid::Document should auto includes itself in a has_many query
- Closed
- related to
-
MONGOID-3356 Objects loaded through an association should have their inverse association set
- Closed