我尝试下面的codes.i得到错误“nil不能被强制进入BigDecimal”。 任何人都帮助我。谢谢。
迁移文件
class AddPaidAmountToVoucher < ActiveRecord::Migration
def self.up
add_column :vouchers, :paid_amount, :decimal, :precision => 18, :scale => 2
end
def self.down
remove_column :vouchers, :paid_amount
end
end
模型类(voucher_payment.rb)
after_update :update_voucher_paid_amount
def update_voucher_paid_amount
voucher = self.voucher
paidAmount = 0
voucher.voucher_payments.each do |voucher_payment|
paidAmount += voucher_payment.paid_amount if voucher_payment.is_approved == true
end
voucher.paid_amount = paidAmount
voucher.save
end
答案 0 :(得分:1)
在循环voucher.voucher_payments
时,一个/一些voucher_payment的pay_amount为零。
voucher_payment.paid_amount = nil
因此,当您将paidAmount添加到nil值时,会出现此错误。