I get the error undefined method 'amount' for nil:NilClass if the Tax (tax_id) is not present on any products.
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
Is there a way I can make it so if there’s no Tax ID present when it searches all of the products, it will render it as nil and just do self.sum(:amount)?
Alternatively, you could scope the products to only map those that actually have a
tax_id. Something like: