I have a Seller model that has_many Items.
I want to get the total sale price of all of a Seller's items.
In seller.rb I have
def total_item_cost
items.to_a.sum(&:sale_price)
end
This works fine if all the items have a sale price.
However, if they haven't been sold yet, sale_price
is nil and the total_item_cost
breaks.
In my app, sale_price
can be either a nil or a zero.
In my total_item_cost
method, how can I treat nil
values as zeros?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…