# Find\_by\_column\_name and for loop returns undef method \`each'

**URL:** <https://www.ruby-forum.com/t/find-by-column-name-and-for-loop-returns-undef-method-each/101010>\
**Category:** Rails\
**Created:** [August 2, 2006, 7:48pm UTC](https://www.ruby-forum.com/t/find-by-column-name-and-for-loop-returns-undef-method-each/101010 "2006-08-02T19:48:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [August 2, 2006, 7:48pm UTC](https://www.ruby-forum.com/t/find-by-column-name-and-for-loop-returns-undef-method-each/101010/1 "2006-08-02T19:48:26Z")

</div>

I’m trying to understand how to select only certain rows in my database  
using RoR.

As a test I try to find certain clients by adding this code in my list  
view…

\<% clientlist = Client.find\_by\_client\_status\_id(1) %\>  
\<% if clientlist %\> \<= This prevents me from getting nil errors.  
\<% for client in clientlist %\> \<= This seems to try and call ‘each’  
method.  
…

 and data display …  
\<% end %\>  
\<% end %\>

My clients table has a client\_status\_id column that I’m trying to use.  
It has values for each client such as 1, 2, 3 that corresponde with a  
client\_status table.

If I do Client.find(:all) in the first line I get all rows from the  
database so everythng else seems to be working toherwise.

Once I figure out the correct find\_by syntax I think I should move it  
into a client model method also.

Any ideas?

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [August 2, 2006, 10:49pm UTC](https://www.ruby-forum.com/t/find-by-column-name-and-for-loop-returns-undef-method-each/101010/2 "2006-08-02T22:49:19Z")

</div>

Guest wrote:

> I’m trying to understand how to select only certain rows in my database  
> using RoR.

Couple things here.

First, use your controller to do your queries.

def show\_clients  
@clientlist = Client.find\_all\_by\_client\_status\_id(1)  
end

Notice find\_all is the variant you want here, not find\_by, to get a list  
of all clients matching your status ID of 1. If there are none, you get  
an empty array, not nil.

Now in your .rhtml you can do this:

\<% for client in clientlist %\>

 ... \<% end %\>

Make sense?

Jeff

> **[Hey Daddio!](http://softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [August 3, 2006, 3:25am UTC](https://www.ruby-forum.com/t/find-by-column-name-and-for-loop-returns-undef-method-each/101010/3 "2006-08-03T03:25:24Z")

</div>

Thanks Jeff - Adding the show\_clients method into my  
clients\_controller.rb didn’t work right off, even if I put it within the  
def list method. Placing it in application\_help.rb or clients\_helper.rb  
is working though.

I also removed the "@clientlist = " and just call “for clients in  
show\_clients” to get the list.

Thanks for the pointer. I seems to be working now.

If it’s more proper to place the query into the controller rather than a  
helper is there something different I need to do to get it working  
there? My application\_helper.rb is getting swollen with lot’s of little  
find(:all) queries and I’d like to do as right as possible.

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [August 3, 2006, 1:04pm UTC](https://www.ruby-forum.com/t/find-by-column-name-and-for-loop-returns-undef-method-each/101010/4 "2006-08-03T13:04:15Z")

</div>

find\_by returns only one record… and .each is a method of array Same  
as  
find :first, :condition…

find\_all\_by\_client\_status should work for you 🙂

Good luck
