# Using both :include and :joins in find()

**URL:** <https://www.ruby-forum.com/t/using-both-include-and-joins-in-find/156222>\
**Category:** Rails\
**Created:** [December 22, 2008, 11:44pm UTC](https://www.ruby-forum.com/t/using-both-include-and-joins-in-find/156222 "2008-12-22T23:44:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [December 22, 2008, 11:44pm UTC](https://www.ruby-forum.com/t/using-both-include-and-joins-in-find/156222/1 "2008-12-22T23:44:12Z")

</div>

I would like to do this:

Greeter.find(:all, :select =\> “DISTINCT user\_id”, :include =\> [:user],  
:joins =\> [:user], :order =\> “users.nickname”)

But an error is always thrown, I either do the join so that I go  
order on the join table… which I have to do… or I do eager loading  
which helps out a lot with the efficiency of the page load time… but  
I can not do both it seems… or can I?

–  
John K.  
removed\_email\_address@domain.invalid

Blog: [http://www.kopanas.com](http://www.kopanas.com)  
Conference: [http://www.cusec.net](http://www.cusec.net)  
Twits: [http://www.twitter.com/kopanas](http://www.twitter.com/kopanas)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [December 23, 2008, 12:05am UTC](https://www.ruby-forum.com/t/using-both-include-and-joins-in-find/156222/2 "2008-12-23T00:05:51Z")

</div>

On 22 Dec 2008, at 22:43, John K. wrote:

> I can not do both it seems… or can I?

In such a case just the :include would create a join.

Fred

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [December 23, 2008, 12:14am UTC](https://www.ruby-forum.com/t/using-both-include-and-joins-in-find/156222/3 "2008-12-23T00:14:04Z")

</div>

It would be nice to know what error gets thrown. I suspect it has  
something to do with using :select in a find where you also  
use :include, but I may be wrong. As far as I know, :select is ignored  
when you used :include.  
Another potential error is that you’re joining the users table twice:  
once on the :include (a left outer join), and once with the :joins (an  
inner join). The generated SQL would not contain aliases for the  
joined table, so it is specifying users twice. Is it your DBMS  
throwing the error?

From the look of your :select, it seems like what you want back is  
User objects, not Greeter objects. If so, why are you querying using  
“Greeter.find”, instead of “User.find”?

This could probably be what you’re looking for:

User.all(:joins =\> :greeter, :order =\> ‘users.nickname’) #get all  
users who also are greeters.

or

Greeter.all(:include =\> :users, :order =\> ‘users.nickname’) #get all  
greeters, and eager load the users.

What is it? No idea!

HTH,

-H
