# Find\_all on an array of ActiveRecords

**URL:** <https://www.ruby-forum.com/t/find-all-on-an-array-of-activerecords/109935>\
**Category:** Rails\
**Created:** [July 20, 2007, 12:45pm UTC](https://www.ruby-forum.com/t/find-all-on-an-array-of-activerecords/109935 "2007-07-20T12:45:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [July 20, 2007, 12:45pm UTC](https://www.ruby-forum.com/t/find-all-on-an-array-of-activerecords/109935/1 "2007-07-20T12:45:35Z")

</div>

In my project, an account has\_many entries and an entry  
has\_and\_belongs\_to\_many tags.  
I want to find all of the entries for an account which are tagged with  
“some specific tag”

The code which I thought would do this is:  
@entries = @account.entries.find\_all{|entry|entry.tags.collect{|tag|  
tag.name}.include?(@tag.name)}

However, this returns an array containing all entries for the account,  
and ignores the include? condition.

While attempting to debug the problem, I simplified the expression:

@entries = @account.entries.find\_all{|entry|false}

This too returns an array containing all entries for the account.  
@account.entries.class is an Array, so I assume the find\_all which I  
am calling is Enum#find\_all and not ActiveRecord#find\_all (whic have  
much different functionality!)

Can anybody point out where I am wrong?

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [July 20, 2007, 1:02pm UTC](https://www.ruby-forum.com/t/find-all-on-an-array-of-activerecords/109935/2 "2007-07-20T13:02:37Z")

</div>

I think this should do it:

@entries = @account.entries.find(:all, :include =\> :tags, :conditions =\>  
[“tags.name = ?”, @tag.name])

Stijn

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [July 20, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/find-all-on-an-array-of-activerecords/109935/3 "2007-07-20T13:16:01Z")

</div>

Thank you both, this is very helpful

---

<div class="post-metadata">

**Author:** ![bitsmith](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@bitsmith](https://www.ruby-forum.com/u/bitsmith)\
**Post date:** [July 20, 2007, 1:09pm UTC](https://www.ruby-forum.com/t/find-all-on-an-array-of-activerecords/109935/4 "2007-07-20T13:09:01Z")

</div>

I think it indeed IS the ActiveRecord#find\_all Method, for the  
following reason:

if you call the find\_all method directly on the association like:  
@account.entries.find\_all  
without the entries being eager-loaded before, it does an AR#find\_all  
because @account.entries doesn’t hold any objects yet.  
This would also explain why the block isn’t evaluated: AR#find\_all  
doesn’t take a block.

Rails aliases Enumerable#find with #detect, and #find\_all with  
#select:

@entries = @account.entries.select{|entry|entry.tags.collect{|tag|  
tag.name}.include?(@tag.name)}

this would also work:  
@entries = @account.entries.to\_a.find\_all{|entry|entry.tags.collect{|  
tag| tag.name}.include?(@tag.name)}

or, use AR for the whole operation:  
@entries = @account.entries.find(:all, :include =\> :tags, :conditions  
=\> [“tags.name = ?”,@tag.name])  
(untested)

Less code, less records fetched by SQL …
