Hello, I am following the guide in AWDWR for the shopping cart in
Chapter 8 Iteration C2. I have the the cart and cart_item models set up
as shown below, but am receiving the error 'undefined method `find' for
#<CartItem:0xb6e7d644>' when I try to add to my cart. Here is the
extract:
def add_product(product)
current_item = @items.find {|item| item.product == product}
if current_item
current_item.increment_quantity
else
@items << CartItem.new(product)
end
end
end
And here is the CartItem model:
class CartItem
attr_reader :product, :quantity
def initialize(product)
@product = product
@quantity = 1
end
def increment_quantity
@quantity += 1
end
def title
@product.title
end
def price
@product.price * @quantity
end
end
Can somebody point me in the right direction as to what is throwing this
error? I tried creating a method called 'find' in the CartItem model
just to see if that did anything, but didn't have much luck.
Never mind...found the problem. Although I am storing the session
information in my database, it seems as though running 'rake
db:sessions:clear' did what it is supposed to do, but there was somehow
session data in my browser yet. According to the book, one only needs
to run the rake task and then refresh the page.
Once I actually closed my browser and reopened it, everything worked as
designed. Perhaps I have misread how storing the session data in the
database instead of cookies works...Is there something that changed with
Rails 2.0.2?
Never mind...found the problem. Although I am storing the session
information in my database, it seems as though running 'rake
db:sessions:clear' did what it is supposed to do, but there was
somehow
session data in my browser yet. According to the book, one only needs
to run the rake task and then refresh the page.
Once I actually closed my browser and reopened it, everything worked
as
designed. Perhaps I have misread how storing the session data in the
database instead of cookies works...Is there something that changed
with
Rails 2.0.2?
Yes - by default in rails 2.0.2 the entire session is in the cookie.
On the way you've found out the pitfall of putting complex objects in
the session: you can get nonsensical and confusing results when the
code changes but the user still has an old session with the old model.