nil不能被强制转换为BigDecimal - Ruby On Rails(rails 2.3.2)

时间:2017-03-31 06:42:43

标签: ruby-on-rails

我尝试下面的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

1 个答案:

答案 0 :(得分:1)

在循环voucher.voucher_payments时,一个/一些voucher_payment的pay_amount为零。

voucher_payment.paid_amount = nil

因此,当您将paidAmount添加到nil值时,会出现此错误。