# Why the preference for find(:all)?

**URL:** <https://www.ruby-forum.com/t/why-the-preference-for-find-all/84757>\
**Category:** Rails\
**Created:** [February 7, 2007, 2:56pm UTC](https://www.ruby-forum.com/t/why-the-preference-for-find-all/84757 "2007-02-07T14:56:40Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [February 7, 2007, 2:56pm UTC](https://www.ruby-forum.com/t/why-the-preference-for-find-all/84757/1 "2007-02-07T14:56:40Z")

</div>

Hello,  
I am just wondering if someone could offer an explanation as to why  
the preferred way to find records is to use find(:all), and why  
find\_all is going the way of the dinosaur? I have been implementing  
BDD in my code (via rSpec), and the find\_all call is far easier to use  
in stubbing/mocking (no need to investigate parameters).  
What advantages does the find(:all) method offer that I have not yet  
seen? Consistency can’t be the answer, as all of the dynamic finders  
use this same syntax. Please help me understand this decision.  
Thanks.

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [February 7, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/why-the-preference-for-find-all/84757/2 "2007-02-07T15:16:42Z")

</div>

Isn’t there a find\_first counterpart as well? It seems as though they  
were consistent before, and changing to this parameterized syntax has  
gained us nothing.

-Chris

On Feb 7, 9:13 am, Alan F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [February 7, 2007, 3:13pm UTC](https://www.ruby-forum.com/t/why-the-preference-for-find-all/84757/3 "2007-02-07T15:13:01Z")

</div>

Chris H. wrote:

> Hello,  
> I am just wondering if someone could offer an explanation as to why  
> the preferred way to find records is to use find(:all), and why  
> find\_all is going the way of the dinosaur? I have been implementing  
> BDD in my code (via rSpec), and the find\_all call is far easier to use  
> in stubbing/mocking (no need to investigate parameters).  
> What advantages does the find(:all) method offer that I have not yet  
> seen? Consistency can’t be the answer, as all of the dynamic finders  
> use this same syntax. Please help me understand this decision.  
> Thanks.

I suspect consistency _is_ the answer. find(:all) and find(:first) are  
the two alternatives.

Alan

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [February 7, 2007, 3:18pm UTC](https://www.ruby-forum.com/t/why-the-preference-for-find-all/84757/4 "2007-02-07T15:18:42Z")

</div>

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

> Thanks.  
> Flexibility. The old find\_first/find\_all methods used positional  
> arguments. At the time, they only supported conditions, joins, and  
> order, but #find now supports :include, :select, etc.

–  
Rick O.  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [February 7, 2007, 3:25pm UTC](https://www.ruby-forum.com/t/why-the-preference-for-find-all/84757/5 "2007-02-07T15:25:46Z")

</div>

So changing to symbolized parameters was found to be easiest to do if  
the entire method was changed? I admit I haven’t had much experience  
with gently deprecating functionality, but I would think it just as  
easy to allow two different types of parameter lists. Anyway, at  
least I have my answer. Thanks for the fast responses.

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [February 7, 2007, 3:32pm UTC](https://www.ruby-forum.com/t/why-the-preference-for-find-all/84757/6 "2007-02-07T15:32:38Z")

</div>

I’m not sure if it is THE reason, but I rationalized it as a need to  
separate the ActiveRecord find\_all method from the Enumerable find\_all  
method. Take for instance the following setup:

class Employer \< ActiveRecord::Base  
has\_many :employees  
end

class Employee \< ActiveRecord::Base  
belongs\_to :employer  
end

Employer.find(:first).employees.find\_all(…)

employees is an ActiveRecord collection, which is an Enumerable. I can  
see times were times I want the employees.find\_all to be the  
Enumerable.find\_all instead of the ActiveRecord find\_all, and the other  
way around as well.

-Lonnie

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [February 7, 2007, 4:27pm UTC](https://www.ruby-forum.com/t/why-the-preference-for-find-all/84757/7 "2007-02-07T16:27:56Z")

</div>

My collegue has a point to add to Lonnie’s remarks: isn’t there also  
an Enumerable.find ? How is a potential confound avoided for this  
method?

-Chris

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [February 7, 2007, 4:47pm UTC](https://www.ruby-forum.com/t/why-the-preference-for-find-all/84757/8 "2007-02-07T16:47:07Z")

</div>

Chris wrote:

> My collegue has a point to add to Lonnie’s remarks: isn’t there also  
> an Enumerable.find ? How is a potential confound avoided for this  
> method?
> 
> -Chris

I stand corrected and completely agree. You’d think after I took the  
time to type out that response that I would have seen the  
Enumerable.find method right next to the find\_all method. As I look  
back into some of my code, I see that I used the Enumerable.detect  
method (of which Enumerable.find is a synonym for) to avoid the  
confusion I was having.

-Lonnie

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [February 8, 2007, 8:19am UTC](https://www.ruby-forum.com/t/why-the-preference-for-find-all/84757/9 "2007-02-08T08:19:30Z")

</div>

Chris wrote:

> So, I am still left wondering: why was find made not only to support  
> different kinds of parameters, but also modify its behavior depending  
> on these parameters, instead of simply supporting different kinds of  
> parameters under the existing find\_all? Wouldn’t the latter scenario  
> make more sense, and be just as flexible?

I agree that this is a rather baffling aspect of Rails’ design. I don’t  
like having to look at the parameters to determine whether a single item  
or a collection will be returned.

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [February 7, 2007, 5:45pm UTC](https://www.ruby-forum.com/t/why-the-preference-for-find-all/84757/10 "2007-02-07T17:45:07Z")

</div>

So, I am still left wondering: why was find made not only to support  
different kinds of parameters, but also modify its behavior depending  
on these parameters, instead of simply supporting different kinds of  
parameters under the existing find\_all? Wouldn’t the latter scenario  
make more sense, and be just as flexible?

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [February 8, 2007, 9:17am UTC](https://www.ruby-forum.com/t/why-the-preference-for-find-all/84757/11 "2007-02-08T09:17:37Z")

</div>

Hello Chris,

2007/2/7, Chris H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I am just wondering if someone could offer an explanation as to why  
> the preferred way to find records is to use find(:all), and why  
> find\_all is going the way of the dinosaur? I have been implementing  
> BDD in my code (via rSpec), and the find\_all call is far easier to use  
> in stubbing/mocking (no need to investigate parameters).  
> What advantages does the find(:all) method offer that I have not yet  
> seen? Consistency can’t be the answer, as all of the dynamic finders  
> use this same syntax. Please help me understand this decision.

I don’t know the exact answer, but for mocking/stubbing, I don’t  
necessarily stub/mock find itself. Instead, I mock #find\_all\_by\_title  
instead.

## Hope that helps !

François Beausoleil  
[http://blog.teksol.info/](http://blog.teksol.info/)  
[http://piston.rubyforge.org/](http://piston.rubyforge.org/)
