# :joins VS :include =\> different results?

**URL:** <https://www.ruby-forum.com/t/joins-vs-include-different-results/189536>\
**Category:** Rails\
**Created:** [June 2, 2010, 6:09pm UTC](https://www.ruby-forum.com/t/joins-vs-include-different-results/189536 "2010-06-02T18:09:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [June 2, 2010, 6:09pm UTC](https://www.ruby-forum.com/t/joins-vs-include-different-results/189536/1 "2010-06-02T18:09:44Z")

</div>

Hi there,

assume that:

1 we have the following situation:

Book :has\_many Pages :has\_many Illustrations  
==== ===== =============

2 certain pages have NO illustrations

3 the following 2 cases:

A) Book.find(:all,  
:joins =\> [:pages =\> [:illustrations]])

B) Book.find(:all,  
:include =\> [:pages =\> [:illustrations]])

Question: Is it correct that:

- in case A), the books containing pages WITHOUT any illustrations will  
NOT show up in the results
- whereas in case B), these books WILL show up?

(Personally, I thought that the main difference between :joins and  
:include was the “eager loading” part.)

Thanks for any explanation for this!  
Tom

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [June 2, 2010, 7:18pm UTC](https://www.ruby-forum.com/t/joins-vs-include-different-results/189536/2 "2010-06-02T19:18:39Z")

</div>

On Jun 2, 5:09 pm, Tom Ha [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Question: Is it correct that:
> 
> - in case A), the books containing pages WITHOUT any illustrations will  
> NOT show up in the results
> - whereas in case B), these books WILL show up?
> 
> (Personally, I thought that the main difference between :joins and  
> :include was the “eager loading” part.)

:joins does an inner join, :include doesn’t (and may not even do a  
join at all)

Fred

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [June 2, 2010, 9:14pm UTC](https://www.ruby-forum.com/t/joins-vs-include-different-results/189536/3 "2010-06-02T21:14:18Z")

</div>

> :joins does an inner join, :include doesn’t (and may not even do a  
> join at all)

If you say “may not”, when would it do or not do a join, then? And which  
type of join?

Doesn’t :include amount to a LEFT JOIN, actually?

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [June 2, 2010, 9:29pm UTC](https://www.ruby-forum.com/t/joins-vs-include-different-results/189536/4 "2010-06-02T21:29:45Z")

</div>

IME :include does a join if you are using the ‘included’ table in the  
query conditions:

A.find(:all, :conditions =\> ‘a.column=1’, :include =\> ‘b’) # no join  
A.find(:all, :conditions =\> ‘a.column=1 AND b.column=2’, :include =\>  
‘b’) # join

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [June 2, 2010, 10:23pm UTC](https://www.ruby-forum.com/t/joins-vs-include-different-results/189536/5 "2010-06-02T22:23:51Z")

</div>

> A.find(:all, :conditions =\> ‘a.column=1 AND b.column=2’, :include =\>  
> ‘b’) # join

And if you say “join”, you mean a “LEFT join” here, if I got the story  
right…

Thanks, guys!
