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

Callbacks in embedded documents (via accepts_nested_attributes_for) was broken since Mongoid 3.0.10?

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

      Why the following spec passes in Mongoid 3.0.9 and fails in Mongoid 3.0.10+ ?

      require 'spec_helper'
      
      class People
        include Mongoid::Document
      
        field :name, type: String
      
        embeds_one :photoset, cascade_callbacks: true
        accepts_nested_attributes_for :photoset
      end
      
      class Photoset
        include Mongoid::Document
        embedded_in :people
      
        field :name, type: String
      
        embeds_many :pictures, cascade_callbacks: true
        accepts_nested_attributes_for :pictures, allow_destroy: true
      end
      
      class Picture
        include Mongoid::Document
        embedded_in :photoset
      
        field :_id, type: Integer
        field :path, type: String
      
        before_destroy { $_picture_before_destroy_called = true }
        after_destroy { $_picture_after_destroy_called = true }
      
        attr_accessible :_id, :path
      end
      
      
      describe People do
        before :all do
          People.delete_all
        end
      
        it 'should work!' do
          $_picture_before_destroy_called = false
          $_picture_after_destroy_called = false
      
          doc = People.new({name: 'John Smith'})
          doc.photoset = Photoset.new({name: 'bar'})
          doc.photoset.pictures << Picture.new({_id: 1, path: 'wherever 1'})
          doc.photoset.pictures << Picture.new({_id: 2, path: 'wherever 2'})
          doc.photoset.pictures << Picture.new({_id: 3, path: 'wherever 3'})
          doc.save!
      
          doc = People.find_by({name: 'John Smith'})
          doc.update_attributes({
            photoset_attributes: {
              pictures_attributes: [
                {_id: 1, path: 'foo 1'}, 
                {_id: 2, path: 'foo 2', _destroy: 1}, 
                {_id: 3, path: 'foo 3'}
              ]
            }
          })
          doc.reload
      
          $_picture_before_destroy_called.should be_true
          $_picture_after_destroy_called.should be_true
          doc.attributes['photoset']['pictures'].should == [
            {"_id" => 1, "path" => "foo 1"}, 
            {"_id" => 3, "path" => "foo 3"}
          ]
        end
      end
      

      Probably, it happened after commit dfe40a3ac985b83fd404d020aaeb3113069be295 or am I doing something wrong?

            Assignee:
            Unassigned Unassigned
            Reporter:
            happysoft Vadim Voitsiakh
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: