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:
app/models/cart.rb:10:in add_product' app/controllers/store_controller.rb:9:in
add_to_cart’
-e:2:in `load’
-e:2
Here is my Cart model:
class Cart
attr_reader :items
def initialize
@items = []
end
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.