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

Dirty tracking of embedded doc cleared in after_save for new record

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

      I observed this today in v2.3.4 and master. Let's assume the following models:

      class Parent
        include Mongoid::Document
        embeds_many :children, class_name: 'Parent::Child'
        after_save do
          puts self.children.map(&:new?)
          puts self.children.map(&:changed?)
          puts self.children.map(&:changes)
        end
      
        class Child
          include Mongoid::Document
        end
      end
      

      Now the following happens for a new record:

      p = Parent.new
      p.children.new
      p.save
      # => false
      # => false
      # => {}
      

      When doing the same on an existing record gives the expected results:

      p = Parent.create
      p.children.new
      p.save
      # => true
      # => true
      # => {"_id"=>[nil, BSON::ObjectId('4f0385a3bebaab3487000014')]}
      

      This seems to be a bug to me.

      Can anyone else reproduce this?

            Assignee:
            Unassigned Unassigned
            Reporter:
            fphilipe fphilipe
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: