# Problem with find with :joins and :include in same call

**URL:** <https://www.ruby-forum.com/t/problem-with-find-with-joins-and-include-in-same-call/167642>\
**Category:** Rails\
**Created:** [May 10, 2009, 9:24pm UTC](https://www.ruby-forum.com/t/problem-with-find-with-joins-and-include-in-same-call/167642 "2009-05-10T21:24:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [May 10, 2009, 9:24pm UTC](https://www.ruby-forum.com/t/problem-with-find-with-joins-and-include-in-same-call/167642/1 "2009-05-10T21:24:58Z")

</div>

Hi all,

I am investigating moving our app from Rails 2.2.2 to Rails 2.3.2. So  
far doesn’t look too bad, but I am running in to one problem.

In a model I have the following named scope:  
named\_scope :valid, {:include =\> :broker, :joins =\> ‘INNER JOIN  
`payers` ON `payers`.customer\_id = `brokers`.id AND  
`payers`.customer\_type = ‘Broker’’, :conditions =\> "payers.status =  
‘active’ and ticket\_sets.delete\_flag = 0 and ticket\_sets.qty \> 0 and  
(brokers.ei\_id is NULL or (brokers.ei\_id is not null and  
ticket\_sets.ei\_id is not null)) "}

This worked perfectly in Rails 2.2.2, but in Rails 2.3.2 this blows up.

Looking at the log file, it appears that the :include clause isn’t being  
parsed.

SELECT `ticket_sets`.\* FROM `ticket_sets` INNER JOIN `payers` ON  
`payers`.customer\_id = `brokers`.id AND `payers`.customer\_type =  
‘Broker’ WHERE (payers.status = ‘active’ and ticket\_sets.delete\_flag =  
0 and ticket\_sets.qty \> 0 and (brokers.ei\_id is NULL or (brokers.ei\_id  
is not null and ticket\_sets.ei\_id is not null)) )

If I remove the the joins clause, the brokers get included like they  
should be, but it doesn’t have payers referenced.

One option would be into use nested includes to include the payers; but  
this has some serious performance issues. The ticket sets table is large  
and we don’t need to the payers data returned, just for selection.

Does anybody have any ideas?

Thanks,

Andrew

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [May 10, 2009, 9:33pm UTC](https://www.ruby-forum.com/t/problem-with-find-with-joins-and-include-in-same-call/167642/2 "2009-05-10T21:33:01Z")

</div>

On May 10, 8:24 pm, Andrew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> One option would be into use nested includes to include the payers; but  
> this has some serious performance issues. The ticket sets table is large  
> and we don’t need to the payers data returned, just for selection.
> 
> Does anybody have any ideas?

it looks like a bug in the code that determines which include strategy  
should be used

Fred

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [May 10, 2009, 9:34pm UTC](https://www.ruby-forum.com/t/problem-with-find-with-joins-and-include-in-same-call/167642/3 "2009-05-10T21:34:34Z")

</div>

Frederick C. wrote:

> On May 10, 8:24ï¿½pm, Andrew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > One option would be into use nested includes to include the payers; but  
> > this has some serious performance issues. The ticket sets table is large  
> > and we don’t need to the payers data returned, just for selection.
> > 
> > Does anybody have any ideas?
> 
> it looks like a bug in the code that determines which include strategy  
> should be used
> 
> Fred

Fred,

Do you want me to file a bug at lighthouse?

Thanks,

Andrew

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [May 10, 2009, 9:48pm UTC](https://www.ruby-forum.com/t/problem-with-find-with-joins-and-include-in-same-call/167642/4 "2009-05-10T21:48:23Z")

</div>

Filed at Lighthouse as #2631.

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [May 11, 2009, 4:14pm UTC](https://www.ruby-forum.com/t/problem-with-find-with-joins-and-include-in-same-call/167642/5 "2009-05-11T16:14:28Z")

</div>

i think only include or joins should be used not both…

On Mon, May 11, 2009 at 1:18 AM, Andrew S. \<

---

<div class="post-metadata">

**Author:** ![starmaerker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9bcb4/32.png) [@starmaerker](https://www.ruby-forum.com/u/starmaerker)\
**Post date:** [May 11, 2009, 4:54pm UTC](https://www.ruby-forum.com/t/problem-with-find-with-joins-and-include-in-same-call/167642/6 "2009-05-11T16:54:28Z")

</div>

On May 11, 3:13 pm, Lakshmi N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> i think only include or joins should be used not both…

in general, nothing wrong with using both (for distinct associations -  
wouldn’t want to include and join the same association)

Fred
