Agile Web Development with Rails, erratum

from the Agile Web Development with Rails, Third Edition. (fourth printing), in Chapter 8.3 iteration C2: Creating a Smarter Cart (page 104), when creating the initial CartItem model class, the price function is defined thusly :

1
2
3
    def price
      @product.price = @quantity
    end

Unfortunately that sets the price to the number of items set in the cart (if you add the item 3 times the price is 3, not $99.88). It should be :

1
2
3
def price
    @product.price * @quantity
  end

or you could do

1
2
3
def price
      @product.price
end

If you do it the second way, the total_price needs to be adjusted (8.5 Iteration C4:finishing the Cart, page 114) be modifying the total_price function from:

1
2
3
   def total_price
      @items.sum { |item| item.price }      
    end

to the correct :

1
2
3
   def total_price
      @items.sum { |item| item.price * item.quantity }      
    end

see http://www.pragprog.com/titles/rails3/errata for more corrections of this otherwise great guide.

Leave a Reply