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

eager loading(includes) for many-to-one does't respect first/last

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.2
    • Affects Version/s: None
    • Component/s: None

      mongoid 3.0.1

      I want to get first post with its blog, but the eager loading fetches all posts' blogs

      Post.includes(:blog).first
      

      <pre>MOPED: 127.0.0.1:27017 QUERY database=blog_dev collection=posts selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 fields=nil (0.2942ms)
      MOPED: 127.0.0.1:27017 QUERY database=blog_dev collection=posts selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=0 skip=0 fields=

      {"blog_id"=>1} (0.9415ms)
      MOPED: 127.0.0.1:27017 GET_MORE database=blog_dev collection=posts limit=0 cursor_id=1656386615102804572 (1.2517ms)
      MOPED: 127.0.0.1:27017 QUERY database=blog_dev collection=blogs selector={"_id"=>{"$in"=>["500e398c6abd0e3f1b000095", "500e398c6abd0e3f1b00009d", "500e398c6abd0e3f1b0000a8", ...]}} flags=[] limit=0 skip=0 fields=nil (2.3420ms)</pre>

      The expected behavior is like the following query
      Post.includes(:blog).limit(1).first
      

      <pre>MOPED: 127.0.0.1:27017 QUERY database=blog_dev collection=posts selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 fields=nil (0.2904ms)
      MOPED: 127.0.0.1:27017 QUERY database=blog_dev collection=posts selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=1 skip=0 fields={"blog_id"=>1}

      (0.1476ms)
      MOPED: 127.0.0.1:27017 QUERY database=blog_dev collection=blogs selector={"_id"=>{"$in"=>["500e398c6abd0e3f1b000095"]}} flags=[] limit=0 skip=0 fields=nil (0.1788ms)</pre>

      BTW, the behavior of eager loading for one-to-many is right

            Assignee:
            durran Durran Jordan
            Reporter:
            qianthinking qianthinking
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: