# Undefined method \`each' for

**URL:** <https://www.ruby-forum.com/t/undefined-method-each-for/82645>\
**Category:** Rails\
**Created:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/undefined-method-each-for/82645 "2007-01-19T16:31:59Z")\
**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:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/undefined-method-each-for/82645/1 "2007-01-19T16:31:59Z")

</div>

Hi all. I am very new to ruby rails and I got this error.

undefined method `each’ for #Video:0x370bdd8 in selectaction.rhtml  
that calls video controller  
3:

  
4:   
5: \<% x = 0 %\>  
6: \<% for video in @videos %\>  
7: 

| \<%= link\_to(image\_tag(“videos/#{video.video\_thumbnail}”,  
8: :size =\> ‘132x99’,  
9: :border =\> 5,

c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/base.rb:1792:in  
`method_missing' #{RAILS_ROOT}/app/views/videos/selectaction.rhtml:6:in`\_run\_rhtml\_videos\_selectaction’

In videos\_controller  
def selectaction  
@videos = Video.find\_by\_subject(“action”)  
end

Now I didn’t define find\_by\_subject, but doesn’t rails do that for me  
if I have a database table called video with with subject as a field?  
And does ruby define video for me?

 |

---

<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, 10:33pm UTC](https://www.ruby-forum.com/t/undefined-method-each-for/82645/2 "2007-09-25T22:33:05Z")

</div>

That worked. Thank you so much.

---

<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, 10:34pm UTC](https://www.ruby-forum.com/t/undefined-method-each-for/82645/3 "2007-09-25T22:34:39Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Hi all. I am very new to ruby rails and I got this error.
> 
> undefined method `each’ for #Video:0x370bdd8 in selectaction.rhtml  
> that calls video controller  
> 3:
> 
>   
> 4:   
> 5: \<% x = 0 %\>  
> 6: \<% for video in @videos %\>  
> [snip]  
> In videos\_controller  
> def selectaction  
> @videos = Video.find\_by\_subject(“action”)  
> end
> 
> Video.find\_by\_subject will only return 1 record. @videos then gets set  
> to a single Video, and when you try to loop through a single video  
> rather than a collection of them, you get 'undefined method ‘each’.
> 
> You want: @videos = Video.find\_all\_by\_subject(“action”)
> 
> Dan M.
> 
> |
> | |
