Solved

undefined method `each' for xxx error

Posted on 2007-11-16
3
1,594 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
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 gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now