# How to SELECT from multiple talbes with AR#find and associat

**URL:** <https://www.ruby-forum.com/t/how-to-select-from-multiple-talbes-with-ar-find-and-associat/99439>\
**Category:** Rails\
**Created:** [July 22, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/how-to-select-from-multiple-talbes-with-ar-find-and-associat/99439 "2006-07-22T19:09:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@alder](https://www.ruby-forum.com/u/alder)\
**Post date:** [July 22, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/how-to-select-from-multiple-talbes-with-ar-find-and-associat/99439/1 "2006-07-22T19:09:17Z")

</div>

So I want to SELECT from multiple tables, so as to be able to do  
“SELECT FROM foo, bar WHERE foo.id=bar.id AND bar.baz=23”.

How do I do it with AR#find?

Also, I need this for associations. has\_many et. al. have :finder\_sql,  
which is nice, but feels like a Pyrrhic: it’s basically doing the  
association in pure SQL, defeating the purpose of AR.

---

<div class="post-metadata">

**Author:** ![alder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@alder](https://www.ruby-forum.com/u/alder)\
**Post date:** [July 22, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/how-to-select-from-multiple-talbes-with-ar-find-and-associat/99439/2 "2006-07-22T19:35:34Z")

</div>

Look at include option in find method.

Model.find(:all, :include =\> [“other\_model”])

---

<div class="post-metadata">

**Author:** ![alder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@alder](https://www.ruby-forum.com/u/alder)\
**Post date:** [July 22, 2006, 8:13pm UTC](https://www.ruby-forum.com/t/how-to-select-from-multiple-talbes-with-ar-find-and-associat/99439/3 "2006-07-22T20:13:53Z")

</div>

On 7/22/06, Bruno C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Look at include option in find method.

> From the docs:

“:include: Names associations that should be loaded alongside using  
LEFT OUTER JOINs. The symbols named refer to already defined  
associations. See eager loading under Associations.”

So it wasn’t intended for this but… it works! 🙂

My guess is that the :include adds table bar to the SELECT, so WHERE  
can refer to it in the same query when you specify it in conditions.

Using it qualifies as a hack, imho 🙂
