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

update_attributes does not reflect changes to embedded documents in the attributes hash

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 8.0.1
    • Affects Version/s: 5.0.2
    • Component/s: Associations, Persistence
    • None
    • Environment:
      Rails 4.2.3
    • Major Change

      When using update_attributes, changes to embedded documents are not immediately reflected in the parent document's attributes hash. Reloading from MongoDB updates the attributes hash.

      Example:

      class Jam
        include Mongoid::Document
        embedded_in :womp
      end
      
      class Womp
        include Mongoid::Document
        embeds_many :jams
        accepts_nested_attributes_for :jams, allow_destroy: true
      end
      
      w = Womp.create!
      
      w.attributes
      #=> {"_id"=>BSON::ObjectId('58489d22a54d75c13c000006')}
      
      w.update_attributes({ jams_attributes: [ {} ] })
      
      w.attributes
      #=> {"_id"=>BSON::ObjectId('58489d22a54d75c13c000006')}
      
      w.reload.attributes
      #=> {"_id"=>BSON::ObjectId('58489d22a54d75c13c000006'), "jams"=>[{"_id"=>BSON::ObjectId('58489d22a54d75c13c000007')}]}
      
      w.update_attributes({ jams_attributes: [ { id: w.jams.first.id, _destroy: true } ] })
      
      w.attributes
      #=> {"_id"=>BSON::ObjectId('58489d22a54d75c13c000006'), "jams"=>[{"_id"=>BSON::ObjectId('58489d22a54d75c13c000007')}]}
      
      w.reload.attributes
      #=> {"_id"=>BSON::ObjectId('58489d63a54d75c13c000008'), "jams"=>[]}
      

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            wyattisimo Jared Wyatt
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: