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

Eager loading doesn't work when mapping twice

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 6.0.1, 5.1.5
    • Affects Version/s: 5.1.4
    • Component/s: None
    • None

      Suppose I have the following two models

      ```ruby
      class Post
      belongs_to :user
      end

      class User; end
      ```

      If I try to `map` a criteria twice, the second time the eager loading is not respected. For example:

      ```
      > crit = Post.limit(7).includes(:user)
      > crit.map

      { |r| r.user.try(:id) }

      ;
      ```

      Will generate the db calls

      ```
      db.find | STARTED | {"find"=>"posts", "filter"=>{}, "limit"=>7}
      db.find | SUCCEEDED | 0.001872s
      db.killCursors | STARTED |

      {"killCursors"=>"posts", "cursors"=>[817538945765]}

      db.killCursors | SUCCEEDED | 0.000116s
      db.find | STARTED | {"find"=>"user", "filter"=>{"_id"=>{"$in"=>[BSON::ObjectId('55e5974764643400033a0000'), BSON::ObjectId('55e597456464340003280000'), nil, BSON::ObjectId('55e597436464340003050000'), BSON::ObjectId('55e5974664643400032f0000'), BSON::ObjectId(...
      db.find | SUCCEEDED | 0.00046s
      ```

      But calling the previous line once more generates an N+1 query

      ```
      > crit.map

      { |r| r.user.try(:id) }

      ;
      ```

      ```
      db.find | STARTED | {"find"=>"posts", "filter"=>{}, "limit"=>7}
      db.find | SUCCEEDED | 0.001755s
      db.find | STARTED | {"find"=>"user", "filter"=>

      {"_id"=>BSON::ObjectId('55e5974764643400033a0000')}

      , "sort"=>{"_id"=>1}}
      db.find | SUCCEEDED | 0.00036199999999999996s
      db.find | STARTED | {"find"=>"user", "filter"=>

      {"_id"=>BSON::ObjectId('55e5974764643400033a0000')}

      , "sort"=>{"_id"=>1}}
      db.find | SUCCEEDED | 0.000387s
      db.find | STARTED | {"find"=>"user", "filter"=>

      {"_id"=>BSON::ObjectId('55e597456464340003280000')}

      , "sort"=>{"_id"=>1}}
      db.find | SUCCEEDED | 0.00029299999999999997s
      db.find | STARTED | {"find"=>"user", "filter"=>

      {"_id"=>BSON::ObjectId('55e597436464340003050000')}

      , "sort"=>{"_id"=>1}}
      db.find | SUCCEEDED | 0.000312s
      db.find | STARTED | {"find"=>"user", "filter"=>

      {"_id"=>BSON::ObjectId('55e5974664643400032f0000')}

      , "sort"=>{"_id"=>1}}
      db.find | SUCCEEDED | 0.00035000000000000005s
      db.find | STARTED | {"find"=>"user", "filter"=>

      {"_id"=>BSON::ObjectId('55e5974764643400033a0000')}

      , "sort"=>{"_id"=>1}}
      db.find | SUCCEEDED | 0.000346s
      db.find | STARTED | {"find"=>"user", "filter"=>

      {"_id"=>BSON::ObjectId('55e5974564643400031f0000')}

      , "sort"=>{"_id"=>1}}
      db.find | SUCCEEDED | 0.0008539999999999999s
      db.killCursors | STARTED |

      {"killCursors"=>"posts", "cursors"=>[816157481622]}

      db.killCursors | SUCCEEDED | 7.7e-05s
      ```

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            mmun Martin Muñoz
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: