# Undefined method \`each' for \*\*\*:Class PROBLEM

**URL:** <https://www.ruby-forum.com/t/undefined-method-each-for-class-problem/107760>\
**Category:** Rails\
**Created:** [June 26, 2007, 12:42am UTC](https://www.ruby-forum.com/t/undefined-method-each-for-class-problem/107760 "2007-06-26T00:42:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 26, 2007, 12:42am UTC](https://www.ruby-forum.com/t/undefined-method-each-for-class-problem/107760/1 "2007-06-26T00:42:56Z")

</div>

Hi,

i am very new to ROR, this is my 2nd day since i solve the problem of  
database connection. when i started to write some codes, i got stuck.  
here is the situation and please give me some help.

i have a “posts” table in my database

CREATE TABLE `mapapp`.`posts` (  
`id` int(11) NOT NULL auto\_increment,  
`title` varchar(255) default NULL,  
`body` text,  
PRIMARY KEY (`id`)  
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

i have an empty “post.rb” in my Models which looks like this:  
class Post \< ActiveRecord::Base  
end

i wrote an action in my controller which looks like this:  
def testing  
@posts = Post  
for p in @posts  
render :text =\> p.id  
end  
end

when i run this action on the browser, it gives me this error message:  
NoMethodError in PlcController#testing  
undefined method `each’ for Post:Class  
RAILS\_ROOT: ./script/…/config/…

Application Trace | Framework Trace | Full Trace  
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active\_record/  
base.rb:1235:in `method_missing' #{RAILS_ROOT}/app/controllers/plc_controller.rb:40:in`testing’  
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active\_record/  
base.rb:1235:in `method_missing' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ base.rb:1095:in`send’  
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/  
base.rb:1095:in `perform_action_without_filters' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ filters.rb:632:in`call\_filter’  
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/  
filters.rb:619:in `perform_action_without_benchmark' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ benchmarking.rb:66:in`perform\_action\_without\_rescue’  
C:/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ benchmarking.rb:66:in`perform\_action\_without\_rescue’  
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/  
rescue.rb:83:in `perform_action' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ base.rb:430:in`send’  
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/  
base.rb:430:in `process_without_filters' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ filters.rb:624:in`process\_without\_session\_management\_support’  
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/  
session\_management.rb:114:in `process' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ base.rb:330:in`process’  
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:41:in  
`dispatch' C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb: 113:in`handle\_dispatch’  
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick\_server.rb:79:in  
`service' C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in`service’  
C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run' C:/ruby/lib/ruby/1.8/webrick/server.rb:173:in`start\_thread’  
C:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start' C:/ruby/lib/ruby/1.8/webrick/server.rb:162:in`start\_thread’  
C:/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start' C:/ruby/lib/ruby/1.8/webrick/server.rb:92:in`each’  
C:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start' C:/ruby/lib/ruby/1.8/webrick/server.rb:23:in`start’  
C:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start' C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:63:in`dispatch’  
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/servers/  
webrick.rb:59  
C:/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:27:in  
`gem_original_require' C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in`require’  
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active\_support/  
dependencies.rb:495:in `require' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ dependencies.rb:342:in`new\_constants\_in’  
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active\_support/  
dependencies.rb:495:in `require' C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/server.rb:39 C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in`gem\_original\_require’  
C:/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:27:in  
`require' script/server:3 C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ base.rb:1235:in`method\_missing’  
#{RAILS\_ROOT}/app/controllers/plc\_controller.rb:40:in `testing' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ base.rb:1095:in`send’  
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/  
base.rb:1095:in `perform_action_without_filters' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ filters.rb:632:in`call\_filter’  
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/  
filters.rb:619:in `perform_action_without_benchmark' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ benchmarking.rb:66:in`perform\_action\_without\_rescue’  
C:/ruby/lib/ruby/1.8/benchmark.rb:293:in `measure' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ benchmarking.rb:66:in`perform\_action\_without\_rescue’  
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/  
rescue.rb:83:in `perform_action' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ base.rb:430:in`send’  
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/  
base.rb:430:in `process_without_filters' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ filters.rb:624:in`process\_without\_session\_management\_support’  
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action\_controller/  
session\_management.rb:114:in `process' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ base.rb:330:in`process’  
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:41:in  
`dispatch' C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb: 113:in`handle\_dispatch’  
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick\_server.rb:79:in  
`service' C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in`service’  
C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run' C:/ruby/lib/ruby/1.8/webrick/server.rb:173:in`start\_thread’  
C:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start' C:/ruby/lib/ruby/1.8/webrick/server.rb:162:in`start\_thread’  
C:/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start' C:/ruby/lib/ruby/1.8/webrick/server.rb:92:in`each’  
C:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start' C:/ruby/lib/ruby/1.8/webrick/server.rb:23:in`start’  
C:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start' C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:63:in`dispatch’  
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/servers/  
webrick.rb:59  
C:/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:27:in  
`gem_original_require' C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in`require’  
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active\_support/  
dependencies.rb:495:in `require' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ dependencies.rb:342:in`new\_constants\_in’  
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active\_support/  
dependencies.rb:495:in `require' C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/server.rb:39 C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in`gem\_original\_require’  
C:/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:27:in  
`require’  
script/server:3

i m very new, so please excuse me if i asked stupid questions.

but thanks for you time looking.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 26, 2007, 2:34am UTC](https://www.ruby-forum.com/t/undefined-method-each-for-class-problem/107760/2 "2007-06-26T02:34:05Z")

</div>

removed\_email\_address@domain.invalid wrote:

> undefined method `each’ for Post:Class  
> Your:

```
 for p in @posts

```

is equivalent to:

```
 @posts.each do |p|

```

You’ve just set @posts to be equal to the class Post, which doesn’t have  
an each method.

What you probably meant to do is:

```
 @posts = Post.find(:all)

```

or some other query to retrieve an array of Posts.

Pete Y.

> **[Pete Yandell — Principal Engineer / Architect](https://notahat.com/)**
>
> Pete Yandell is an experienced Principal Software Engineer and Architect based in Melbourne, Australia.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 25, 2007, 11:01pm UTC](https://www.ruby-forum.com/t/undefined-method-each-for-class-problem/107760/3 "2007-09-25T23:01:05Z")

</div>

Make the following change…

def testing  
@posts = Post.find(:all)
