# Each.do error with one record

**URL:** <https://www.ruby-forum.com/t/each-do-error-with-one-record/122454>\
**Category:** Rails\
**Created:** [December 7, 2007, 6:50pm UTC](https://www.ruby-forum.com/t/each-do-error-with-one-record/122454 "2007-12-07T18:50:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anthony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/49beb7/32.png) [@anthony](https://www.ruby-forum.com/u/anthony)\
**Post date:** [December 7, 2007, 6:50pm UTC](https://www.ruby-forum.com/t/each-do-error-with-one-record/122454/1 "2007-12-07T18:50:04Z")

</div>

Hello, I realize that the reason I am getting this error is because my  
code returns many records when there is more than one to be found.  
However, if it only finds one, the each.do flakes out and gives me a  
NoMethodError. What is the best way to handle it?

# – Find all events for a given school

@events = Event.find\_by\_school\_id(params[:school\_id], :order =\>  
‘date’)

# – each.do code to loop through each one:
\<% @events.each do |e|%\> \<% end %\>

| Time | Title | Description |
| --- | --- | --- |
| \<%= e.date.strftime('%I:%M %p') %\> | \<%= link\_to e.title, :action =\> 'show', :id =\> e.id %\> | \<%= link\_to e.school.name, :action =\> 'list', :school\_id =\> e.school %\> |

Thank you for you help

---

<div class="post-metadata">

**Author:** ![anthony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/49beb7/32.png) [@anthony](https://www.ruby-forum.com/u/anthony)\
**Post date:** [December 7, 2007, 6:56pm UTC](https://www.ruby-forum.com/t/each-do-error-with-one-record/122454/2 "2007-12-07T18:56:30Z")

</div>

On 12/7/07, Anthony [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello, I realize that the reason I am getting this error is because my  
> code returns many records when there is more than one to be found.  
> However, if it only finds one, the each.do flakes out and gives me a  
> NoMethodError. What is the best way to handle it?
> 
> # – Find all events for a given school
> 
> @events = Event.find\_by\_school\_id(params[:school\_id], :order =\>  
> ‘date’)

I think you want  
@events = Event.find\_all\_by\_school\_id(…)

find\_by\_school\_id only returns the first found.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![anthony](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/49beb7/32.png) [@anthony](https://www.ruby-forum.com/u/anthony)\
**Post date:** [December 7, 2007, 7:04pm UTC](https://www.ruby-forum.com/t/each-do-error-with-one-record/122454/3 "2007-12-07T19:04:43Z")

</div>

That’s it. Thank you.
