Fix calculate_price when order_item without product is provided

This commit is contained in:
benji 2015-09-21 17:38:22 +02:00
parent 0581a7238d
commit e9aa52736a
2 changed files with 2 additions and 2 deletions

View file

@ -36,7 +36,7 @@ class Order < ActiveRecord::Base
private
def calculate_price
self.price_cents = self.order_items.map{ |oi| oi.count * oi.product.price_cents }.sum
self.price_cents = self.order_items.map{ |oi| oi.count * (oi.product.try(:price_cents) || 0) }.sum
end
def create_api_job

View file

@ -48,7 +48,7 @@ describe Order do
describe 'order_items' do
it 'should be validated' do
@order.order_items << OrderItem.new(count: -5)
@order.order_items.build(count: -5)
expect(@order).to_not be_valid
end
end