# Undefined method - what undefined method

**URL:** <https://www.ruby-forum.com/t/undefined-method-what-undefined-method/65222>\
**Category:** Rails\
**Created:** [August 16, 2006, 9:36pm UTC](https://www.ruby-forum.com/t/undefined-method-what-undefined-method/65222 "2006-08-16T21:36:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [August 16, 2006, 9:36pm UTC](https://www.ruby-forum.com/t/undefined-method-what-undefined-method/65222/1 "2006-08-16T21:36:18Z")

</div>

I am doing a typical database list, but when I do  
\<% for m in @private\_messages %\> (Line 28) It tells me that the method  
‘each’ is undefined. What’s wrong, I did not call the method each…  
Here’s the error:  
Showing app/views/account/mail.rhtml where line #28 raised:

undefined method `eachâ?? for #PrivateMessage:0x408bb830

Extracted source (around line #28):

25:

Reply   
26: Delete   
27:   
28: \<% for m in @private\_messages %\>  
29:   
etc…

RAILS\_ROOT: …/config/…  
Application Trace | Framework Trace | Full Trace

#{RAILS\_ROOT}/vendor/rails/activerecord/lib/active\_record/base.rb:1792:in  
`method_missing' #{RAILS_ROOT}/app/views/account/mail.rhtml:28:in`\_run\_rhtml\_account\_mail’

Thanks for your help and time

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [August 16, 2006, 10:33pm UTC](https://www.ruby-forum.com/t/undefined-method-what-undefined-method/65222/2 "2006-08-16T22:33:59Z")

</div>

My guess is that behind the scenes the interpreter translates the ‘in’  
construct to ‘each’ with a block. A little experimentation confirms  
the guess:

class Foo  
def each  
puts ‘Each called’  
end  
end

f=Foo.new

for m in f  
puts m  
end

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [August 16, 2006, 10:39pm UTC](https://www.ruby-forum.com/t/undefined-method-what-undefined-method/65222/3 "2006-08-16T22:39:59Z")

</div>

Ruby has a method where you could do… for example…

@private\_messages.each do |m|

and that would do a for loop, with each object of @private\_messages…  
basically the same thing you’re trying to do with that for loop… I  
assume  
the for loop calls the .each method. Is @private\_messages an array? or  
a  
Hash… if it isn’t, it won’t have the .each method.

www-data-5 wrote:

> #{RAILS\_ROOT}/vendor/rails/activerecord/lib/active\_record/base.rb:1792:in  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
View this message in context:  
[http://www.nabble.com/Undefined-method---what-undefined-method-tf2117354.html#a5840069](http://www.nabble.com/Undefined-method---what-undefined-method-tf2117354.html#a5840069)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [August 17, 2006, 3:06am UTC](https://www.ruby-forum.com/t/undefined-method-what-undefined-method/65222/4 "2006-08-17T03:06:21Z")

</div>

Is @private\_messages an array? or

> a  
> Hash… if it isn’t, it won’t have the .each method.  
> It’s an array, or a database query @private\_messages =  
> PrivateMessage.find\_by\_to\_and\_folder(authemail, folder)

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [August 17, 2006, 4:46am UTC](https://www.ruby-forum.com/t/undefined-method-what-undefined-method/65222/5 "2006-08-17T04:46:31Z")

</div>

You want find\_all\_by\ __. find\_by\__ does a find(:first).

The error message is a giveaway: for #PrivateMessage:0x408bb830  
means it is trying to call .each on a PrivateMessage object, not a  
list of things.

Max
