Solved

undefined method `each' for xxx error

Posted on 2007-11-16
3
1,599 Views
Last Modified: 2012-06-21
Hi Experts,

I have a question displaying result set.
Currently, this works.

in controller:

 @attending_ip = AttendingIp.find_by_sql("SELECT ....")

in view:
<% @attending_ip.each do |u|  %>
...
...

But I am having this error when I do this:

in controller:

@attending_ip = AttendingIp.find(params[:id])

in view:

<% @attending_ip.each do |u|  %>

I tried this, but same error

<% for u in @attending_ip  %>

My guess is that @attending_ip = AttendingIp.find(params[:id]) only returns one row. If I do this <%= @attending_ip.first_name %> without <% @attending_ip.each do |u|  %>, it works. But I want to use this view for the def that returns multiple rows also. I hope my point is clear.
Why am I having this error?

thx,
0
Comment
Question by:dkim18
3 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 350 total points
ID: 20300649
well you're right... find returns an instance of your object (which is not an array) so that is why trying to iterate through them fails.

Off the top of my head, two possible solutions are:

@attending_ip = AttendingIp.find(:all, :conditions => ["id = ?", params[:id]] )

or

make a partial and pass to it your value:

if @attending_ip.is_a?(Array)
   render :partial => "your_partial", :collection => @attending_ip
else
   render :partial => "your_partial", :object => @attending_ip
end


0
 
LVL 1

Expert Comment

by:Computer101
ID: 20632864
Forced accept.

Computer101
EE Admin
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Ruby on Rails Webrat Error 1 431
ECMAScript VS Ruby 3 394
How do you upgrade Ruby? (Windows) 2 215
What does 'resources :users' do in rails 1 300
I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question