# Find(:all) and find\_all\_\<etc\>

**URL:** <https://www.ruby-forum.com/t/find-all-and-find-all-etc/72903>\
**Category:** Rails\
**Created:** [October 19, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/find-all-and-find-all-etc/72903 "2006-10-19T19:12:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7ab992/32.png) [@luke](https://www.ruby-forum.com/u/luke)\
**Post date:** [October 19, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/find-all-and-find-all-etc/72903/1 "2006-10-19T19:12:48Z")

</div>

Perhaps I’m an idiot and just don’t get it, and perhaps this question  
has been asked a million times before, but I’m not really sure how to go  
about searching for an answer to it:

Why does my code Library.find(:all, :conditions =\> “user\_id =  
#{session[:user].id}”) return [] instead of nil when there are no  
results?

---

<div class="post-metadata">

**Author:** ![luke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7ab992/32.png) [@luke](https://www.ruby-forum.com/u/luke)\
**Post date:** [October 19, 2006, 7:20pm UTC](https://www.ruby-forum.com/t/find-all-and-find-all-etc/72903/2 "2006-10-19T19:20:26Z")

</div>

Luke wrote:

> Perhaps I’m an idiot and just don’t get it, and perhaps this question  
> has been asked a million times before, but I’m not really sure how to go  
> about searching for an answer to it:
> 
> Why does my code Library.find(:all, :conditions =\> “user\_id =  
> #{session[:user].id}”) return [] instead of nil when there are no  
> results?

Thats the way its defined in ActiveRecord. It returns an array of models  
for the sql results and returns it. So when there are no matches, it  
returns an empty array.

---

<div class="post-metadata">

**Author:** ![luke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7ab992/32.png) [@luke](https://www.ruby-forum.com/u/luke)\
**Post date:** [October 19, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/find-all-and-find-all-etc/72903/3 "2006-10-19T19:22:42Z")

</div>

Raja Venkataraman wrote:

> Thats the way its defined in ActiveRecord. It returns an array of models  
> for the sql results and returns it. So when there are no matches, it  
> returns an empty array.

I guess my question is: why does ActiveRecord define it this way? It  
would seem to be more consistent with the way everything else works in  
ruby for a function that has no results to return nil, instead of  
returning an empty array.

---

<div class="post-metadata">

**Author:** ![luke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7ab992/32.png) [@luke](https://www.ruby-forum.com/u/luke)\
**Post date:** [October 19, 2006, 7:39pm UTC](https://www.ruby-forum.com/t/find-all-and-find-all-etc/72903/4 "2006-10-19T19:39:37Z")

</div>

for consistency with its normal results, i’d think. it usually returns  
an array, and so code gets written expecting to handle an array,  
therefore it returns an empty array instead of nil.

Luke wrote:

> Perhaps I’m an idiot and just don’t get it, and perhaps this question  
> has been asked a million times before, but I’m not really sure how to go  
> about searching for an answer to it:
> 
> Why does my code Library.find(:all, :conditions =\> “user\_id =  
> #{session[:user].id}”) return [] instead of nil when there are no  
> results?

–  
Lance I.  
Web Applications Developer  
RBS Interactive  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![luke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7ab992/32.png) [@luke](https://www.ruby-forum.com/u/luke)\
**Post date:** [October 19, 2006, 7:47pm UTC](https://www.ruby-forum.com/t/find-all-and-find-all-etc/72903/5 "2006-10-19T19:47:38Z")

</div>

> Raja Venkataraman wrote:
> 
> > Thats the way its defined in ActiveRecord. It returns an array of models  
> > for the sql results and returns it. So when there are no matches, it  
> > returns an empty array.
> 
> I guess my question is: why does ActiveRecord define it this way? It  
> would seem to be more consistent with the way everything else works in  
> ruby for a function that has no results to return nil, instead of  
> returning an empty array.

I would disagree… For me, if I’m doing a find(:all…) I’m going to  
expect that 99% of the time I’m going to get back some data… and odds  
are I’m going to loop through them.

So, by returning an empty array I can do that without having to check if  
the result is nil first and getting that nasty “you tried to call  
nil.each” error.

-philip

---

<div class="post-metadata">

**Author:** ![luke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/7ab992/32.png) [@luke](https://www.ruby-forum.com/u/luke)\
**Post date:** [October 19, 2006, 7:39pm UTC](https://www.ruby-forum.com/t/find-all-and-find-all-etc/72903/6 "2006-10-19T19:39:37Z")

</div>

> Perhaps I’m an idiot and just don’t get it, and perhaps this question  
> has been asked a million times before, but I’m not really sure how to go  
> about searching for an answer to it:
> 
> Why does my code Library.find(:all, :conditions =\> “user\_id =  
> #{session[:user].id}”) return [] instead of nil when there are no  
> results?

Because the docs say so 🙂

Find first: This will return the first record matched by the options  
used.  
These options can either be specific conditions or merely an order. If  
no  
record can matched, nil is returned.

Find all: This will return all the records matched by the options used.  
If  
no records are found, an empty array is returned.
