# Has\_many :through aggregation

**URL:** <https://www.ruby-forum.com/t/has-many-through-aggregation/132299>\
**Category:** Rails\
**Created:** [March 18, 2008, 10:42pm UTC](https://www.ruby-forum.com/t/has-many-through-aggregation/132299 "2008-03-18T22:42:48Z")\
**Posts on this page:** 3\
**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:** [March 18, 2008, 10:42pm UTC](https://www.ruby-forum.com/t/has-many-through-aggregation/132299/1 "2008-03-18T22:42:48Z")

</div>

Hello there,

I’m trying to figure out a way to use the has\_many :through option to  
aggregate collections in a specific way. Here’s an example of the  
issue I’m trying to address:

create\_table :companies do |t|  
t.integer :parent\_id  
…  
end

create\_table :employees do |t|  
t.integer :company\_id  
end

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

class Company \< ActiveRecord::Base  
has\_many :companies, :class\_name =\> ‘Company’, :foreign\_key =\>  
‘parent\_id’  
has\_many :employees  
has\_many :all\_employees, :through =\> :children\_companies, :source  
=\> :employees  
end  
|

A company may own other companies (has\_many :companies). Each company  
has its own set of employees (has\_many :employees). What I’d like to  
be able to do is have a search-able collection of all employees for a  
company (including all the employees at each child company). However,  
has\_many :all\_employees only gives me the employees of the child  
companies, but not the companies proper employees.

If I use the :finder\_sql option I lose finder\_in\_collection on the  
association which is what I’m really after.

Any advice would be immensely appreciated.

Thanks,  
Patrice

---

<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:** [March 19, 2008, 12:05am UTC](https://www.ruby-forum.com/t/has-many-through-aggregation/132299/2 "2008-03-19T00:05:16Z")

</div>

You could grab both employees collection for the current company and  
all the employees collections for all of the children companies the  
“has\_many :all\_employees” and aggregate the results (I guess removing  
duplicate entries if someone worked for both companies).

On Mar 18, 2:41 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<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:** [March 19, 2008, 12:31am UTC](https://www.ruby-forum.com/t/has-many-through-aggregation/132299/3 "2008-03-19T00:31:43Z")

</div>

Not sure what you mean by ‘aggregate the results’? Adding the two  
collections together with ‘+’ returns an array that does not respond  
to find(:all, :conditions =\> …).

-Patrice

On Mar 18, 4:04 pm, “removed\_email\_address@domain.invalid” \<ESPN3.DL-
