# Undefined method 'find'

**URL:** <https://www.ruby-forum.com/t/undefined-method-find/130061>\
**Category:** Rails\
**Created:** [February 25, 2008, 3:04am UTC](https://www.ruby-forum.com/t/undefined-method-find/130061 "2008-02-25T03:04:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![djknorr75](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/71e660/32.png) [@djknorr75](https://www.ruby-forum.com/u/djknorr75)\
**Post date:** [February 25, 2008, 3:04am UTC](https://www.ruby-forum.com/t/undefined-method-find/130061/1 "2008-02-25T03:04:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![djknorr75](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/71e660/32.png) [@djknorr75](https://www.ruby-forum.com/u/djknorr75)\
**Post date:** [February 25, 2008, 3:19am UTC](https://www.ruby-forum.com/t/undefined-method-find/130061/2 "2008-02-25T03:19:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![djknorr75](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/71e660/32.png) [@djknorr75](https://www.ruby-forum.com/u/djknorr75)\
**Post date:** [February 25, 2008, 1:52pm UTC](https://www.ruby-forum.com/t/undefined-method-find/130061/3 "2008-02-25T13:52:14Z")

</div>

On 25 Feb 2008, at 02:19, Derek Knorr wrote:

> 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.

Fred
