Hi guys, I going through the ''Agile Web Development with Rails'' book and I''m stuck on a small annoying problem. The following code adds a product to a shopping cart. If the cart already has the product in then it will update its quanity by one. However, it doesn''t do as it says. It adds a new line item still! Please help. Cheers, Nick. ------------------------------------------------------------------------------- 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
Nick Rowlands said:> Hi guys, > > I going through the ''Agile Web Development with Rails'' book and I''m > stuck on a small annoying problem. The following code adds a product > to a shopping cart. If the cart already has the product in then it > will update its quanity by one. However, it doesn''t do as it says. It > adds a new line item still!I''m sure the authors themselves will provide you a good answer in a bit, but for now I''ll try my hand.> item = @items.find {|i| i.product_id == product.id}My hunch would be that the product_id method of LineItem may not be returning what you think. In other words it would seem this find always fails, and so i.product_id and product.id never seem to be equal. Ryan
A quick trip to the books errata (http://books.pragprog.com/titles/rails/errata, see no 81) informs me that the section I rightly skipped should in fact not be skipped. There problem went away by changing application.rb to: class ApplicationController < ActionController::Base model :cart model :line_item end Don''t really understand what''s going on here but the book suggests that the session data is somehow getting lost, even though the cart has state? Thanks for your help though. Shame the book has just gone to the printers, lots of errors, though I suppose you never get a perfect book. At least I know the errata web page exists. Cheers, Nick. On 19/07/05, Ryan Leavengood <mrcode-br1l8tUfTSesTnJN9+BGXg@public.gmane.org> wrote:> Nick Rowlands said: > > Hi guys, > > > > I going through the ''Agile Web Development with Rails'' book and I''m > > stuck on a small annoying problem. The following code adds a product > > to a shopping cart. If the cart already has the product in then it > > will update its quanity by one. However, it doesn''t do as it says. It > > adds a new line item still! > > I''m sure the authors themselves will provide you a good answer in a bit, > but for now I''ll try my hand. > > > item = @items.find {|i| i.product_id == product.id} > > My hunch would be that the product_id method of LineItem may not be > returning what you think. In other words it would seem this find always > fails, and so i.product_id and product.id never seem to be equal. > > Ryan >-- ___________________________ http://dailydurham.blogspot.com
Nick Rowlands wrote:> A quick trip to the books errata > (http://books.pragprog.com/titles/rails/errata, see no 81) informs me > that the section I rightly skipped should in fact not be skipped. > > There problem went away by changing application.rb to: > > class ApplicationController < ActionController::Base > model :cart > model :line_item > end > > Don''t really understand what''s going on here but the book suggests > that the session data is somehow getting lost, even though the cart > has state?See here [1] and here [2] [1] http://wiki.rubyonrails.com/rails/show/sessions [2] http://wiki.rubyonrails.com/rails/show/HowtoAvoidSessionRestoreError R. -- http://robinbowes.com If a man speaks in a forest, and his wife''s not there, is he still wrong?