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

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

      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

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

              Created:
              Updated:
              Resolved: