Ruby on Rails - 回调after_destroy未运行

时间:2017-09-25 19:02:10

标签: ruby-on-rails ruby ruby-on-rails-4

我有很多关系,我需要destroy的回调才能在另一个库中建立连接并删除那里的对象,但是当我从连接表中删除一个对象时不执行destroy < / p>

  

萨拉

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 个答案:

没有答案