# Subselects and associations(:include)

**URL:** <https://www.ruby-forum.com/t/subselects-and-associations-include/156207>\
**Category:** Rails\
**Created:** [December 22, 2008, 8:07pm UTC](https://www.ruby-forum.com/t/subselects-and-associations-include/156207 "2008-12-22T20:07:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arvias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@arvias](https://www.ruby-forum.com/u/arvias)\
**Post date:** [December 22, 2008, 8:07pm UTC](https://www.ruby-forum.com/t/subselects-and-associations-include/156207/1 "2008-12-22T20:07:42Z")

</div>

Hi.

Is there a way to stop rails from tokenizing a mysql query? I have an  
association where I need to change :select to:

:select =\> “\*, (select count(something\_id) from second\_table where  
second\_table.something\_id = main\_table.id ) as count”

It works OK without loading associations, however I need to add  
an :include. At that point all the select is changed and it says  
‘count’ does not exist.

How can this made to work?

–  
M.

---

<div class="post-metadata">

**Author:** ![arvias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@arvias](https://www.ruby-forum.com/u/arvias)\
**Post date:** [December 22, 2008, 11:40pm UTC](https://www.ruby-forum.com/t/subselects-and-associations-include/156207/2 "2008-12-22T23:40:05Z")

</div>

On 22 Dec 2008, at 19:07, Marcelo B. wrote:

> an :include. At that point all the select is changed and it says  
> ‘count’ does not exist.

> How can this made to work?

It can’t. joins based :include overwrites the select. The non joined  
based include doesn’t but rails won’t use it because it sees you  
referencing tables other than the main table in the query (and isn’t  
smart enough to work out that it’s ok because it’s just a subselect).

Fred

---

<div class="post-metadata">

**Author:** ![arvias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@arvias](https://www.ruby-forum.com/u/arvias)\
**Post date:** [December 24, 2008, 12:26am UTC](https://www.ruby-forum.com/t/subselects-and-associations-include/156207/3 "2008-12-24T00:26:55Z")

</div>

On 23 Dec 2008, at 21:43, TomRossi7 wrote:

> Have you tried using the :joins key and manually specifying your  
> joins?

It depends on what you’re trying to achieve. If you want to eager load  
stuff the :joins won’t do it. If you just want to join some tables  
because you need them for your conditions etc… then :joins is  
exactly what is needed.

Fred

---

<div class="post-metadata">

**Author:** ![arvias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@arvias](https://www.ruby-forum.com/u/arvias)\
**Post date:** [December 23, 2008, 10:44pm UTC](https://www.ruby-forum.com/t/subselects-and-associations-include/156207/4 "2008-12-23T22:44:51Z")

</div>

Have you tried using the :joins key and manually specifying your  
joins?

On Dec 22, 5:39 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
