# For in loop with only one element not working

**URL:** <https://www.ruby-forum.com/t/for-in-loop-with-only-one-element-not-working/169535>\
**Category:** Rails\
**Created:** [June 4, 2009, 10:59pm UTC](https://www.ruby-forum.com/t/for-in-loop-with-only-one-element-not-working/169535 "2009-06-04T22:59:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dbc845/32.png) [@Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/u/Ruby_on_RailsS_Talk)\
**Post date:** [June 4, 2009, 10:59pm UTC](https://www.ruby-forum.com/t/for-in-loop-with-only-one-element-not-working/169535/1 "2009-06-04T22:59:12Z")

</div>

Hi

I have the following code:

@lunchtimes = Lunchtime.find\_by\_dayname("#{todayname}")  
for l in @lunchtimes do  
# i = lunch.lunchfrom  
# j = lunch.lunchto  
# while i \< j do  
# id="#{session[:dateviewing]}/#{i.strftime(’%H:%M:00’)}/#  
{lunch.therapist\_id}"  
# page[id].replace “

LUNCH”  
# end  
# i += 15.minutes  
end

I’ve put the hashes in to see where the code is failing BTW!

All I’m trying to do is loop through the search results from  
@lunchtimes. At the moment the query is returning only one record:

=\> #\<Lunchtime id: 4, therapist\_id: 1, dayname: “Thursday”, lunchfrom:  
“2000-01-01 12:00:00”, lunchto: “2000-01-01 13:00:00”, effective:  
“2009-06-04”, created\_at: nil, updated\_at: nil\>

And, as such I’m getting the following error:

NoMethodError (undefined method `each' for #<Lunchtime:0x21c1c70>): /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/ attribute_methods.rb:260:in`method\_missing’  
/app/controllers/appointment\_controller.rb:178:in  
`\_\_instance\_exec0’

With more than one returned search results it works …

I know it’s probably something simple … but can anyone tell me where  
I’m going wrong. I’m sure similar code is working in other parts of my  
program.

Thanks in advance for your help.

Darren

---

<div class="post-metadata">

**Author:** ![Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/dbc845/32.png) [@Ruby\_on\_RailsS\_Talk](https://www.ruby-forum.com/u/Ruby_on_RailsS_Talk)\
**Post date:** [June 4, 2009, 11:29pm UTC](https://www.ruby-forum.com/t/for-in-loop-with-only-one-element-not-working/169535/2 "2009-06-04T23:29:09Z")

</div>

2009/6/4 Ruby on Rails: Talk [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Â Â # Â Â Â Â Â id="#{session[:dateviewing]}/#{i.strftime(‘%H:%M:00’)}/#  
> @lunchtimes. At the moment the query is returning only one record:  
> Â Â /app/controllers/appointment\_controller.rb:178:in  
> `\_\_instance\_exec0’
> 
> With more than one returned search results it works …

I am not an expert in this but I understood that find\_by\_attribute  
returned only the first matching record, in which case the result  
would never be an array. If you want to get all of them then you  
should use find\_all\_by\_attribute and the result will always be an  
array, possibly with only one element.

I may well be wrong on this though.

Colin
