# Undefined method "xxx" of a model when calling a helper

**URL:** <https://www.ruby-forum.com/t/undefined-method-xxx-of-a-model-when-calling-a-helper/67390>\
**Category:** Rails\
**Created:** [September 3, 2006, 1:02pm UTC](https://www.ruby-forum.com/t/undefined-method-xxx-of-a-model-when-calling-a-helper/67390 "2006-09-03T13:02:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [September 3, 2006, 1:02pm UTC](https://www.ruby-forum.com/t/undefined-method-xxx-of-a-model-when-calling-a-helper/67390/1 "2006-09-03T13:02:37Z")

</div>

Hi all

I’m creating a shop page. I have a cart model that looks like the  
following:

class Cart  
attr\_reader :line\_items

def get\_line\_item id  
@line\_items.each do |l|  
return l if l.id == id  
end  
end

def initialize  
@line\_items = []  
end

def empty!  
initialize  
end

def empty?  
return true if @line\_items.empty?  
false  
end  
end

And I have some helper methods in application\_helper.rb:

module ApplicationHelper  
def find\_cart  
session[:cart] ||= Cart.new  
end

def empty\_cart  
find\_cart.empty!  
end

def cart\_empty?  
return true if find\_cart.empty?  
end  
end

Then I want to display a partial form within the layout  
application.rhtml but only if the cart is not empty:

\<%= render(:partial =\> ‘warenkorb/warenkorb’, :locals =\> {:cart =\>  
find\_cart}) unless cart\_empty? %\>

But this displays me the following error:

undefined method `empty?’ for #\<Cart:0x230020c @line\_items=[]\>

Where does this error come from? I just don’t get it, the Cart object is  
instantiated when calling find\_cart, and the Cart class definitely does  
have a method called “empty?”!

Thanks for help,  
Joshua

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [September 3, 2006, 5:13pm UTC](https://www.ruby-forum.com/t/undefined-method-xxx-of-a-model-when-calling-a-helper/67390/2 "2006-09-03T17:13:04Z")

</div>

Joshua M. wrote:

> class Cart  
> …  
> def empty?  
> return true if @line\_items.empty?  
> false  
> end  
> end
> 
> And I have some helper methods in application\_helper.rb:
> 
> module ApplicationHelper  
> def cart\_empty?  
> return true if find\_cart.empty?  
> end  
> end

Not sure why you’re getting that error. Are you sure you dont have a  
nother file redefing the cart class somewhere? It’s coming from  
“find\_cart.empty?” in your helper method.

Also, I’m not sure if this will help, but those methods could a whole  
lot simpler. in your empty? method, @line\_items.empty? already returns  
true or false so it can be greatly simplified to:

def empty?  
@line\_items.empty?  
end

And the same with your cart\_empty? helper. But why use a helper for  
that when you can ask the cart directly if its empty? It’s less code  
and easier to troubleshoot.

\<%= render :partial … unless find\_cart.empty? %\>

Lastly, do all the other method on your cart object work ok?

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [September 4, 2006, 2:43pm UTC](https://www.ruby-forum.com/t/undefined-method-xxx-of-a-model-when-calling-a-helper/67390/3 "2006-09-04T14:43:38Z")

</div>

Hi –

On Sun, 3 Sep 2006, Joshua M. wrote:

> @line\_items.each do |l|  
> return l if l.id == id  
> end  
> end

```
def get_line_item(id)
  @line_items.find {|l| l.id == id }
end

```

> false  
> end

```
def empty?
  @line_items.empty?
end

```

> end
> 
> And I have some helper methods in application\_helper.rb:
> 
> module ApplicationHelper  
> def find\_cart  
> session[:cart] ||= Cart.new

It seems a little odd to me to be manipulating the session data in a  
view (or view helper). You might want to find a way to juggle things  
so that this is back in the controller.

> end
> 
> def empty\_cart  
> find\_cart.empty!  
> end
> 
> def cart\_empty?  
> return true if find\_cart.empty?  
> end

```
def cart_empty?
  find_cart.empty?
end

```

> undefined method `empty?’ for #\<Cart:0x230020c @line\_items=[]\>
> 
> Where does this error come from? I just don’t get it, the Cart object is  
> instantiated when calling find\_cart, and the Cart class definitely does  
> have a method called “empty?”!

I’m afraid I can’t duplicate your error. I’ve tried to make up for it  
with a few code hints 🙂 (see above) Could the problem have  
something to do with caching and then changing the model file?

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)
