Solved

undefined method `each' for xxx error

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

813 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

11 Experts available now in Live!

Get 1:1 Help Now