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

Model.update doesn't do a $set and also queries all its has_one relationships

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Resolution: Done
    • Affects Version/s: None
    • Fix Version/s: 3.1.3
    • Component/s: None
    • Labels:
      None

      Description

      class Person
        include Mongoid::Document
        has_one :house
        field :name, :type => String
      end
       
      class House
        include Mongoid::Document
        belongs_to :person
      end
       
      Person.delete_all()
      House.delete_all()
      Mongoid::IdentityMap.clear
       
      p = Person.create
      # => #<Person _id: 512d432edd365a75f4000001, name: nil>
      Mongoid::IdentityMap.clear
      Person.find(p.id).update(:name => "Jon")
      # => true
      p = Person.find(p.id)
      # => #<Person _id: 512d432edd365a75f4000001, name: nil>
      p.reload.name
      # => nil
      

      Here are the Moped logs, you can see that it makes a call to houses. I tested with other has_ones, and it seems like it queries all of them on the update.

      [ 6:20:24.204225000]   MOPED: 127.0.0.1:27017 QUERY        database=development collection=people selector={"_id"=>"512d432edd365a75f4000001"} flags=[:slave_ok] limit=0 skip=0 batch_size=nil fields=nil (0.4830ms)
      [ 6:20:24.206950000]   MOPED: 127.0.0.1:27017 QUERY        database=development collection=houses selector={"$query"=>{"person_id"=>"512d432edd365a75f4000001"}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.8159ms)
      

      Using Mongoid 3.1.1

        Attachments

          Activity

            People

            Assignee:
            Unassigned
            Reporter:
            jonhyman Jon Hyman
            Participants:
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: