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

Using `includes` on the `belongs_to` member of a `has_many` is loaded but not used

      class User
        include Mongoid::Document
        has_many :images
      end
      
      class Image
        include Mongoid::Document
        belongs_to :user
      end
      

      Then eager loading the user off of the image....

      user = User.create
      Image.create(:user => user)
      
      image = Image.includes(:user).first
      # MOPED: ... database=development collection=images selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil (3.6521ms)
      # MOPED: ... database=development collection=users selector={"_id"=>{"$in"=>["514cb621f433ea9332000001"]}} flags=[] limit=0 skip=0 batch_size=nil fields=nil (0.2980ms)
      
      image.user
      
      # MOPED: ... database=development collection=users selector={"$query"=>{"_id"=>"514cb621f433ea9332000001"}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil (0.4160ms)
      

      As you can see the user is queried for off the image load but is then queried for when you actually reference the user.

      # Gemfile
      GIT
        remote: git://github.com/mongoid/mongoid.git
        revision: de244e9c323d777cdf6c260e9461200ad932185b
        specs:
          mongoid (4.0.0)
            activemodel (~> 4.0.0.beta)
            moped (~> 1.4.2)
            origin (~> 1.0)
            tzinfo (~> 0.3.22)
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            bnorton bnorton
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: