Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

undefined method `each' for xxx error

Posted on 2007-11-16
3
Medium Priority
?
1,609 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 1400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

916 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