# Select through proxy objects without joins

**URL:** <https://www.ruby-forum.com/t/select-through-proxy-objects-without-joins/146119>\
**Category:** Rails\
**Created:** [August 18, 2008, 8:27am UTC](https://www.ruby-forum.com/t/select-through-proxy-objects-without-joins/146119 "2008-08-18T08:27:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![daniel304](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@daniel304](https://www.ruby-forum.com/u/daniel304)\
**Post date:** [August 18, 2008, 8:27am UTC](https://www.ruby-forum.com/t/select-through-proxy-objects-without-joins/146119/1 "2008-08-18T08:27:55Z")

</div>

Company has many Departments,  
Department has many Employees.

Is there nice easy way to select all Employees of certain Company? I  
mean using just proxy objects, without joins.

---

<div class="post-metadata">

**Author:** ![daniel304](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@daniel304](https://www.ruby-forum.com/u/daniel304)\
**Post date:** [August 18, 2008, 8:33am UTC](https://www.ruby-forum.com/t/select-through-proxy-objects-without-joins/146119/2 "2008-08-18T08:33:46Z")

</div>

In your model:

app/models/company.rb

has\_many :departments  
has\_many :employees, :through =\> :departments

And then reference via an existing company object:

@company.employees

---

<div class="post-metadata">

**Author:** ![daniel304](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@daniel304](https://www.ruby-forum.com/u/daniel304)\
**Post date:** [August 18, 2008, 8:47am UTC](https://www.ruby-forum.com/t/select-through-proxy-objects-without-joins/146119/3 "2008-08-18T08:47:14Z")

</div>

Thanks, Ryan, you hacked my case 🙂 In fact, I have four objects, also  
Holding has many Companies.  
Can I in the same way select all employees of Holding? I read manual  
about join models in Rails and did not find words about joining  
through two or more entities.

---

<div class="post-metadata">

**Author:** ![daniel304](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@daniel304](https://www.ruby-forum.com/u/daniel304)\
**Post date:** [August 18, 2008, 8:54am UTC](https://www.ruby-forum.com/t/select-through-proxy-objects-without-joins/146119/4 "2008-08-18T08:54:50Z")

</div>

It’s not wise to go through four levels… but if you must. Define a  
method in your holding model:

def employees  
companies.map { |c| c.employees }  
end

---

<div class="post-metadata">

**Author:** ![daniel304](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e5b9ba/32.png) [@daniel304](https://www.ruby-forum.com/u/daniel304)\
**Post date:** [August 18, 2008, 8:58am UTC](https://www.ruby-forum.com/t/select-through-proxy-objects-without-joins/146119/5 "2008-08-18T08:58:21Z")

</div>

Yes, I used such code as my first solution (just don’t forget to  
call .flatten) and now looking for something nicer. Thank you anyway,  
Ryan.
