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

Callback after_destroy & before_destroy in embedded documents is not getting fired

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

      Hi,

      I am using mongoid 2.4.11 and rails 3.2.6. I have before_save and before_destroy callbacks in the embedded documents. after_save works fine but before_destroy is not being fired on the embedded document. The code that I have is

      Unable to find source-code formatter for language: `. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      class Resume
        include Mongoid::Document
        include Mongoid::Timestamps
        field :name, type: String
      
        embeds_one :personal_information, cascade_callbacks: true
        embeds_one :skill, cascade_callbacks: true
        def next_available_position
          self.associations.keys.collect { |association_name| self.send(association_name) }.compact.length
        end
      end
      

      `

      Unable to find source-code formatter for language: `. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      class PersonalInformation
        include Mongoid::Document
        field :full_name, type: String
        field :position, type: Integer
        embedded_in :resume
        before_create :set_position
        before_destroy :reset_positions
        after_destroy :after_reset_positions
      
        protected
        def set_position
          self.position = resume.next_available_position
        end
      
        def reset_positions
          puts "I am in reset_positions"
        end
      
        def after_reset_positions
          puts "I am in after_reset_positions"
        end
      
      end
      

      `

      Unable to find source-code formatter for language: `. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      class Skill
        include Mongoid::Document
        field :content, type: String
        field :position, type: Integer
        embedded_in :resume
        before_create :set_position
        before_destroy :reset_positions
        after_destroy :after_reset_positions
      
        protected
        def set_position
          self.position = resume.next_available_position
        end
      
        def reset_positions
          puts "I am in reset_positions"
        end
      
        def after_reset_positions
          puts "I am in after_reset_positions"
        end
      
      end
      

      `

      To delete the embedded document I am setting the association to nill in my frontend code (backbone.js views). This actually deletes the the embedded document but does not fire the destroy callbacks.

      Its the same case with 3.0.0.rc. Log from 3.0

      Unable to find source-code formatter for language: `. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      Started PUT "/resumes/4fe02008da9d1a73a0000016" for 127.0.0.1 at 2012-06-19 00:34:42 -0700
      Processing by ResumesController#update as JSON
        Parameters: {"resume"=>{"name"=>"New Resume", "created_at"=>"2012-06-19T06:45:28Z", "summary"=>{"_id"=>"4fe024d0da9d1a753e000001", "content"=>nil, "name"=>"Summary", "position"=>1}, "training"=>nil, "updated_at"=>"2012-06-19T06:48:53Z", "id"=>"4fe02008da9d1a73a0000016"}, "id"=>"4fe02008da9d1a73a0000016"}
        MOPED: localhost:27017 COMMAND      database=admin command={:ismaster=>1} (0.0ms)
        MOPED: localhost:27017 COMMAND      database=admin command={:ismaster=>1} (0.0ms)
        MOPED: localhost:27017 QUERY        database=jobmia_development collection=resumes selector={"_id"=>{"$in"=>[4fe02008da9d1a73a0000016]}} flags=[] limit=0 skip=0 fields=nil (0.0ms)
      WARNING: Can't mass-assign protected attributes: id
      WARNING: Can't mass-assign protected attributes: _id
        MOPED: localhost:27017 UPDATE       database=jobmia_development collection=resumes selector={"_id"=>4fe02008da9d1a73a0000016} update={"$unset"=>{"training"=>true}} flags=[] (0.0ms)
      
      

      `

      Thanks,
      Ajay

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

              Created:
              Updated:
              Resolved: