【发布时间】:2016-05-18 17:56:58
【问题描述】:
我在模型中使用 after_destroy 有点麻烦
这是这个:
class Transaction < ActiveRecord::Base
belongs_to :user
delegate :first_name, :last_name, :email, to: :user, prefix: true
belongs_to :project
delegate :name, :thanking_msg, to: :project, prefix: true
validates_presence_of :project_id
after_save :update_collected_amount_in_project
after_update :update_collected_amount_if_disclaimer
after_destroy :update_collected_amount_after_destroy
def currency_symbol
currency = Rails.application.config.supported_currencies.fetch(self.currency)
currency[:symbol]
end
private
def update_collected_amount
new_collected_amount = project.transactions.where(success: true, transaction_type: 'invest').sum(:amount)
project.update_attributes(collected_amount: (new_collected_amount / 100).to_f) # Stored in € not cents inside projects table
end
def update_collected_amount_in_project
update_collected_amount if transaction_type == 'invest' && success == true
end
def update_collected_amount_if_disclaimer
update_collected_amount if transaction_type == 'invest' && self.changes.keys.include?('success') && self.changes.fetch('success', []).fetch(1) == false
end
def update_collected_amount_after_destroy
update_collected_amount
end
end
当我使用类似的东西时:
Transaction.last.delete
它永远不会进入我的 after_destroy,我试图包含一些输出但没有。我不知道我如何使用这个after_destroy 是否错了,我也尝试过before_destroy,我也遇到了同样的问题。 after_save 和 after_update 完美运行。
【问题讨论】:
-
包括像 Pry 或 ByeBug 这样的调试器并设置断点,而不是尝试使用像
puts或Rails.logger这样的东西进行调试。
标签: ruby-on-rails ruby-on-rails-4 activerecord