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

build_<relation> and create_<relation> do not trigger destroy callbacks on replaced record

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

      If I have:

      class Foo
        include Mongoid::Document
        embeds_one :bar
      end
      
      class Bar
        include Mongoid::Document
        embedded_in :foo
        before_destroy :whine
        def whine
         puts "boo hoo"
        end
      end
      

      Then do

      foo = Foo.create
      bar1 = foo.create_bar
      bar2 = foo.create_bar
      

      The on_destroy callback isn't being called for the for bar1. Here it is in irb

      1.9.3p194 :014 > foo = Foo.create
       => #<Foo _id: 4ff7826a73f0577e0e000001, _type: nil> 
      1.9.3p194 :015 > bar1 = foo.create_bar
       => #<Bar _id: 4ff7826b73f0577e0e000002, _type: nil> 
      1.9.3p194 :016 > foo.bar.destroy
      boo hoo
       => true 
      1.9.3p194 :017 > bar1 = foo.create_bar
       => #<Bar _id: 4ff7827973f0577e0e000003, _type: nil> 
      1.9.3p194 :018 > bar2 = foo.create_bar
       => #<Bar _id: 4ff7828173f0577e0e000004, _type: nil> 
      1.9.3p194 :019 > foo.bar._id
       => BSON::ObjectId('4ff7828173f0577e0e000004') 
      

            Assignee:
            durran Durran Jordan
            Reporter:
            eagsalazar eagsalazar
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: