undefined method 'amount' for nil:NilClass
税金(tax_id
)がどの製品にも存在しない場合、エラーが発生します。
class Product < ActiveRecord::Base
attr_accessible :amount, :tax_id
belongs_to :tax
def self.total_with_tax
self.sum(:amount) + all.map(&:tax).map(&:amount).sum
end
end
class Tax < ActiveRecord::Base
attr_accessible :amount
has_many :products
end
すべての商品を検索するときにTaxIDが存在しない場合、nilとしてレンダリングされ、そのまま実行されるようにする方法はありますself.sum(:amount)
か?