Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-2882

Set base relation on has_many enumerable objects

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 7.0.0
    • 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

      {|post| post.person.object_id}

      => [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?

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            danielbeardsley danielbeardsley
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: