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

Loading relation unnecessarily when assigning objects

      Hi,
      I have two models, `Post{{ and }}Comment`

      If I load a `Comment{{ and assign it a different }}Post{{ by doing }}comment.post = otherpost`, mongoid queries the database for the post again.

      >> comment = Comment.last
        MOPED: 127.0.0.1:27017 QUERY        database=peggy_development collection=comments selector={"$query"=>{}, "$orderby"=>{:_id=>-1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 4.3250ms
      => #<Comment _id: fff7cc460000eff7d3ccbbf4, created_at: 2014-03-20 14:47:20 UTC, updated_at: 2014-03-20 14:47:20 UTC, post_id: BSON::ObjectId('fff7cc460000eff7d3ccbbf3')>
      >> post = Post.first
        MOPED: 127.0.0.1:27017 QUERY        database=peggy_development collection=posts selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.5940ms
      => #<Post _id: 5321853e4a6f6a04fa000000, c_at(created_at): 2014-03-13 10:15:26 UTC, u_at(updated_at): 2014-04-08 14:14:10 UTC>
      >> comment.post = post
        MOPED: 127.0.0.1:27017 QUERY        database=peggy_development collection=posts selector={"$query"=>{"_id"=>BSON::ObjectId('fff7cc460000eff7d3ccbbf3')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.7160ms
      => #<Post _id: 5321853e4a6f6a04fa000000, c_at(created_at): 2014-03-13 10:15:26 UTC, u_at(updated_at): 2014-04-08 14:14:10 UTC>
      

      I am running from commit `f0b20eb240` (almost head)

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

              Created:
              Updated:
              Resolved: