Forum: Ruby on Rails SQL joins question

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.
32edf22df6932b252d7be5a1b9b766c9?d=identicon&s=25 Stephen Fagan (railsman)
on 2009-05-01 18:55
I want to link up 3 tables to list all members of a particular group. My
code (which is wrong) looks like this:

@group_members = Membership.find( :all,
                                  :joins => ['FROM memberships INNER
JOIN groups ON memberships.group_id= group.id' + 'INNER JOIN users ON
memberships.user_id=users.id'])


This gives me an "association name not found" error. From my log file
the sql string is as follows:

'FROM memberships INNER JOIN groups ON memberships.group_id=
group.idINNER JOIN users ON memberships.user_id=users.id'

Can anyone please let me know where I'm going wrong - SQL is not my
forté :@/


Thanks


Steve
Da55727b189e6a3ddee2ec69f68e0ffb?d=identicon&s=25 Sj Tib (sktib)
on 2009-05-01 19:02
I don't know the answer but something that jumped out at me was that you
don't have a space in between "group.idINNER". You may want to try it
with something like

:joins => ['FROM memberships INNER JOIN groups ON memberships.group_id=
group.id' + ' INNER JOIN users ON memberships.user_id=users.id'])

Note the single space before second INNER

-S

Stephen Fagan wrote:
> I want to link up 3 tables to list all members of a particular group. My
> code (which is wrong) looks like this:
>
> @group_members = Membership.find( :all,
>                                   :joins => ['FROM memberships INNER
> JOIN groups ON memberships.group_id= group.id' + 'INNER JOIN users ON
> memberships.user_id=users.id'])
>
>
> This gives me an "association name not found" error. From my log file
> the sql string is as follows:
>
> 'FROM memberships INNER JOIN groups ON memberships.group_id=
> group.idINNER JOIN users ON memberships.user_id=users.id'
>
> Can anyone please let me know where I'm going wrong - SQL is not my
> forté :@/
>
>
> Thanks
>
>
> Steve
Ced56202e9039027cfc2da2f70457977?d=identicon&s=25 E. Litwin (Guest)
on 2009-05-01 19:03
(Received via mailing list)
Do you have a space between group.idINNER JOIN users?
In your code and log file, it looks like the space is missing.

On May 1, 9:55 am, Stephen Fagan <rails-mailing-l...@andreas-s.net>
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-05-01 19:51
(Received via mailing list)
On May 1, 5:55 pm, Stephen Fagan <rails-mailing-l...@andreas-s.net>
wrote:
> I want to link up 3 tables to list all members of a particular group. My
> code (which is wrong) looks like this:
>
> @group_members = Membership.find( :all,
>                                   :joins => ['FROM memberships INNER
> JOIN groups ON memberships.group_id= group.id' + 'INNER JOIN users ON
> memberships.user_id=users.id'])
>

this form of sql assumes that you are giving it a list of associaton
names, ie

Membership.find(:all, :joins => [:groups, :users]) looks like it would
do what you are trying to do.

The other form involves sql fragments, in which case you must provide
one such fragment, not an array ie


@group_members = Membership.find( :all,
                                   :joins => 'INNER JOIN groups ON
memberships.group_id= group.id INNER JOIN users ON
memberships.user_id=users.id')


Fred
This topic is locked and can not be replied to.