Forum: Ruby on Rails undefined method `each' for

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9fd462ec8e479e0530eab840221c14aa?d=identicon&s=25 jimjohnlists@yahoo.com (Guest)
on 2007-01-19 16:31
(Received via mailing list)
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: <table>
4:   <tr>
5:   <% x = 0 %>
6:   <% for video in @videos %>
7:     <td><%= 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?
9fd462ec8e479e0530eab840221c14aa?d=identicon&s=25 jimjohnlists@yahoo.com (Guest)
on 2007-09-25 22:33
(Received via mailing list)
That worked. Thank you so much.
B0f09f622c913a13b9bfd332fca56563?d=identicon&s=25 Dan Manges (dcmanges)
on 2007-09-25 22:34
(Received via mailing list)
jimjohnlists@yahoo.com 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: <table>
> 4:   <tr>
> 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
This topic is locked and can not be replied to.