Forum: Ruby on Rails Agile tutorial

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
171ea139761951336b844e708d1547ab?d=identicon&s=25 James B. Byrne (Guest)
on 2006-01-25 20: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
        end
        # 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 == product.id}
                if item
                        item.quantity += 1
                else
                        item = LineItem.for_product(product)
                        @items << item
                end
                @total_price += product.price
        end
end

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 product.id but how and
where product_id is defined in the hash is not clear to me.

--
     *** e-mail is not a secure channel ***
mailto:byrnejb.<token>@harte-lyne.ca
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
C8a634a01a2c4508360874bff7fb1a7f?d=identicon&s=25 Kevin Olbrich (olbrich)
on 2006-01-25 20:47
James B. Byrne wrote:
> item = @items.find {|i| i.product_id == product.id}

Try this instead

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

_Kevin
This topic is locked and can not be replied to.