Re: undefined method `each'' for

Dan Manges
Sun, 14 Jan 2007 05:58:10 -0000

jimjohnlists-/E1597aS9LQAvxtiuMwx3w@public.gmane.org
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 Manges