# How to filter an activerecord find\_all

**URL:** <https://www.ruby-forum.com/t/how-to-filter-an-activerecord-find-all/96898>\
**Category:** Rails\
**Created:** [January 24, 2006, 11:27pm UTC](https://www.ruby-forum.com/t/how-to-filter-an-activerecord-find-all/96898 "2006-01-24T23:27:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 24, 2006, 11:27pm UTC](https://www.ruby-forum.com/t/how-to-filter-an-activerecord-find-all/96898/1 "2006-01-24T23:27:33Z")

</div>

I have a nice hierarchical table structure like this:

divisions  
has\_many groups

groups  
belongs\_to division  
has\_many subgroups

subgroups  
belongs\_to group  
has\_many units

units  
belongs\_to subgroup

I have a report which is based on units, but i want to be able to filter  
the units by which subgroup, or which group, or which division. I also  
want to sort them by division.name,then group.name, then subgroup.name.

What’s the syntax i need? I’ve got this far…  
@units = Unit.find\_all :include =\> XXX, :order =\> XXX

Oh, and another topic - i finally figured out how to deploy rails with  
lighttpd and scgi on a windows server the other day!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 25, 2006, 1:08am UTC](https://www.ruby-forum.com/t/how-to-filter-an-activerecord-find-all/96898/2 "2006-01-25T01:08:18Z")

</div>

chris hulbert wrote:

> I have a nice hierarchical table structure like this:
> 
> divisions  
> has\_many groups
> 
> groups  
> belongs\_to division  
> has\_many subgroups
> 
> subgroups  
> belongs\_to group  
> has\_many units
> 
> units  
> belongs\_to subgroup
> 
> I have a report which is based on units, but i want to be able to filter  
> the units by which subgroup, or which group, or which division. I also  
> want to sort them by division.name,then group.name, then subgroup.name.
> 
> What’s the syntax i need? I’ve got this far…  
> @units = Unit.find\_all :include =\> XXX, :order =\> XXX
> 
> Oh, and another topic - i finally figured out how to deploy rails with  
> lighttpd and scgi on a windows server the other day!

I think this might work if you add it to your find,

:include =\> ‘divisions’,  
:conditions =\> [“divisions.id = ?”, division\_id],  
:order\_by =\> ‘divisions.name DESC’

You may run into trouble with the ‘group’ column name since there is a  
‘group by’ sql command.

You might also want to consider using acts\_as\_tree to handle the  
group/subgroup thing in one table if that makes sense.

\_Kevin

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 25, 2006, 4:58am UTC](https://www.ruby-forum.com/t/how-to-filter-an-activerecord-find-all/96898/3 "2006-01-25T04:58:58Z")

</div>

On 2006-01-24 19:08:18 -0500, Kevin O.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:

> > subgroups
> 
> :order\_by =\> ‘divisions.name DESC’
> 
> You may run into trouble with the ‘group’ column name since there is a  
> ‘group by’ sql command.

Doesn’t Rails escape table and column names with backtics (``) ? If  
not, it should, imo 🙂

Ben

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 25, 2006, 5:09am UTC](https://www.ruby-forum.com/t/how-to-filter-an-activerecord-find-all/96898/4 "2006-01-25T05:09:38Z")

</div>

Benoit G. wrote:

> On 2006-01-24 19:08:18 -0500, Kevin O.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) said:
> 
> > > subgroups
> > 
> > :order\_by =\> ‘divisions.name DESC’
> > 
> > You may run into trouble with the ‘group’ column name since there is a  
> > ‘group by’ sql command.
> 
> Doesn’t Rails escape table and column names with backtics (``) ? If  
> not, it should, imo 🙂
> 
> Ben

It might work fine, but my personal policy is to avoid table names that  
might be mistaken for SQL commands. But then, I’m a belt and suspenders  
kind of guy when it comes to this. BTW, I haven’t tested it to see if  
anything bad happens.

\_Kevin

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 25, 2006, 6:03am UTC](https://www.ruby-forum.com/t/how-to-filter-an-activerecord-find-all/96898/5 "2006-01-25T06:03:28Z")

</div>

Groups isn’t my issue here. Anyway its groupS, plural, so it doesn’t  
interfere with ‘group’ as in ‘group by’. I checked before i used this  
table design - i’m not silly.

But yeah, my question still remains: how to filter a find\_all by a field  
several joins away.

I’ve gotten it working with a find\_by\_sql, but that’s a bit ‘brute  
force’ don’t you think? I’d like something nicer if its possible.
