Undefined method - what undefined method

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

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

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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


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

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)

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