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

Method update nullify the fields of nested documents that are not represented in the hash

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 5.0.1
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      Models:

      class Parent
        embeds_many :children
        accepts_nested_attributes_for :children
      end
      
      class Child
        field first_name
        field last_name
      
        embedded_in :parent
      end
      

      Spec:

      describe 'update' do  
        let(:parent) { Parent.create }
        let(:child) { parent.children.create first_name: 'first', last_name: 'last' }
        subject{parent.update params}
      
        context "params have 'children_attributes' key " do 
          let(:params) { children_attributes: [id: child.id, first_name: 'new']}
          it {expect{subject}.to change{parent.reload.children.first.first_name}.to('new')} # => pass
          it {expect{subject}.not_to change{parent.reload.children.first.last_name}} # => pass
        end 
      
        context "params have 'children_attributes' key "do 
          let(:params) { children: [id: child.id, first_name: 'new']}
          it {expect{subject}.to change{parent.reload.children.first.first_name}.to('new')} # => pass
          it {expect{subject}.not_to change{parent.reload.children.first.last_name}} # => fail. last_name is nil
        end 
      
        context  "children is a hash" do 
          let(:params) { children: {"0" => {id: child.id, first_name: 'new'}}}
          it {expect{subject}.to change{parent.reload.children.first.first_name}.to('new')} # => pass
          it {expect{subject}.not_to change{parent.reload.children.first.last_name}} # => pass
        end 
      end
      

      Bug or feature?

            Assignee:
            dianna.hohensee@mongodb.com Dianna Hohensee (Inactive)
            Reporter:
            max-konin max-konin
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: