Forum: Ruby on Rails Agile tutorial

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
James B. Byrne (Guest)
on 2006-01-25 21:38
(Received via mailing list)
Thank you for the comments on conditional replacements.  I now have
another oddity to deal with.

When adding products to the session cart I am not able to have the
count incremented if the product has already been selected, rather
another item is added to the cart.  I copied the code from the book
as carefully as I could and could find no error but to be sure I
then cut and pasted the code file from the rails development site
and obtained the same result.

my app/models/cart.rb file presently looks like this:

class Cart
        attr_reader :items
        attr_reader :total_price
        def initialize
                @items = []
                @total_price = 0.0
        # add a product to our list of items. If an item already
        # exists for that product, up the count instead
        def add_product(product)
                item = @items.find {|i| i.product_id ==}
                if item
                        item.quantity += 1
                        item = LineItem.for_product(product)
                        @items << item
                @total_price += product.price

Is there any other place that I should be looking? What I am
getting for output is this:

Display Cart

Your cart currently holds 2 items.
1.0 	Development Product 03 	14.34 	14.34
1.0 	Development Product 03 	14.34 	14.34

I realize that this is not much to go on but any pertinent
suggestions that anyone can make would be appreciated.  It is
evident that i.product_id is never equal to but how and
where product_id is defined in the hash is not clear to me.

     *** e-mail is not a secure channel ***
James B. Byrne                Harte & Lyne Limited
vox: +1 905 561 1241          9 Brockley Drive
fax: +1 905 561 0757          Hamilton, Ontario
<token> = hal                 Canada L8E 3C3
Kevin O. (Guest)
on 2006-01-25 21:47
James B. Byrne wrote:
> item = @items.find {|i| i.product_id ==}

Try this instead

item = @items.find {|i| ==}

This topic is locked and can not be replied to.