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

assign_attributes for embedded field raising conflict after assign of empty array

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.3.4
    • Affects Version/s: 7.3.3
    • Component/s: Persistence
    • Labels:
      None
    • Fully Compatible

      Given the following Parent and Child models:

      class Parent
        include Mongoid::Document
        embeds_many :childs
      end
      class Child
        include Mongoid::Document
        embedded_in :parent
      end
      

      Every time you run assign_attributes it sets {{delayed_atomic_sets}}, but when you assign an empty array (parent.assign_attributes(childs: [])) it does not clear {{delayed_atomic_sets}} as expected.

      This causes an error on save due to conflicting operations (set and unset child) if a previous command had set {{delayed_atomic_sets}}.

      Below is a reproduction of this behavior: 

      parent = Parent.create()
      5.times { parent.childs.create() }
      
      parent.assign_attributes(childs: parent.childs.first(4))
      parent.delayed_atomic_sets['childs'].length => 4
      parent.assign_attributes(childs: parent.childs.first(3))
      parent.delayed_atomic_sets['childs'].length => 3
      parent.assign_attributes(childs: [])
      parent.delayed_atomic_sets['childs'].length => 3
      
      parent.save => Updating the path 'childs' would create a conflict at 'childs' (on localhost:27017, legacy retry, attempt 1) (Mongo::Error::OperationFailure)
      

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            vitor.hirata@parafuzo.com Vitor Hirata
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: