Ruby on Rails - Callback after_destroy is not running

Viewed 258

I have a relationship many to many, I need the callback of destroy to be able to make a connection in another bank and delete an object there too, but the destroy is not executed when I delete an object from the join table

Sala

class Sala < ActiveRecord::Base
  belongs_to :empresa
  has_many :sala_participantes, dependent: :destroy
  has_many :participantes, :through => :sala_participantes
end

Sala Participante:

 class SalaParticipante < ActiveRecord::Base
      belongs_to :sala
      belongs_to :participante
      belongs_to :confbridge_pin, dependent: :destroy
      validates :sala, presence: true

      before_create :generate_token
      after_destroy :destroy_confbridge_pin


      def destroy_confbridge_pin
        self.confbridge_pin.destroy
      end

      def generate_token
        token = 4.times.map{ SecureRandom.random_number(10)}.join
        tk = SalaParticipante.where(token: token)
        if !tk.present?
         self.token = token
         cp = ConfbridgePin.create! nome: self.participante.nome,
                               sala: self.sala.nome,
                               pin: self.token
          self.confbridge_pin = cp
        else
          generate_token
        end
      end
    end

Participante

class Participante < ActiveRecord::Base
  belongs_to :empresa
  has_many :sala_participantes
  has_many :salas, :through => :sala_participantes

end
0 Answers
Related