Forum: Ruby on Rails subselects and associations(:include)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3518ff338e5eee9b32eb1815daabc602?d=identicon&s=25 Marcelo Barbudas (Guest)
on 2008-12-22 20:07
(Received via mailing list)
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.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-22 23:40
(Received via mailing list)
On 22 Dec 2008, at 19:07, Marcelo Barbudas 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
5f3d52658aafcf0aabe76a5147e8909f?d=identicon&s=25 TomRossi7 (Guest)
on 2008-12-23 22:44
(Received via mailing list)
Have you tried using the :joins key and manually specifying your
joins?

On Dec 22, 5:39 pm, Frederick Cheung <frederick.che...@gmail.com>
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-24 00:26
(Received via mailing list)
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
This topic is locked and can not be replied to.