Oh, that's the business. Thanks heaps Gabe, that explains a lot.

—Ben


2010/10/22 Gabe Hollombe <[email protected]>

> In my excitement  I pasted the wrong thing from my test console.
>
> Assuming users have many groups through memberships, what you want is:
>
> Membership.where(:group_id => (Membership.select(:group_id).where(:user_id
> => 1).project))
>
>
> Cheers,
> -g
>
>
> On Fri, Oct 22, 2010 at 5:24 PM, Gabe Hollombe <[email protected]> wrote:
>
>> This arel query:
>>
>> Membership.where(:id => (Membership.select(:id).where(:user_id =>
>> 1).project))
>>
>> Gives me this SQL:
>>
>> Membership Load (0.2ms)  SELECT "memberships".* FROM "memberships" WHERE
>> ("memberships"."id" IN (SELECT id FROM "memberships" WHERE
>> ("memberships"."user_id" = 1)))
>>
>> Basically, my understanding of #project, is that it causes the Arel chain
>> you call it on to generate it's SQL at evaluation time.
>>
>> I think that'll work for you =-)
>>
>> -g
>>
>>
>> On Thu, Oct 21, 2010 at 11:15 PM, Bodaniel Jeanes <[email protected]> wrote:
>>
>>> Ah the part about other memberships wasn't clear. The problem is that
>>> memberships is already joined into the query so it needs to be aliased and
>>> then another join performed. I'd do it the way you are doing it now, really.
>>> If you want to make it 1 query you could use @user.board_ids.to_sql to embed
>>> it in the conditions as a sub-query... maybe.
>>>
>>> Bo
>>> [image: Bodaniel Jeanes]
>>>
>>> *Bodaniel Jeanes* [image: LinkedIn] 
>>> <http://www.linkedin.com/in/bjeanes>[image:
>>> Twitter]  <http://twitter.com/bjeanes>[image: Tungle.me] 
>>> <http://tungle.me/bjeanes>[image:
>>> Blog] <http://bjeanes.com>
>>> Whttp://bjeanes.com [email protected]+61412639224
>>> [image: Google Talk] [image: MSN] [image: Google Wave] [email protected] 
>>> [image:
>>> Skype][image: AIM] bojeanes
>>>
>>>
>>>
>>>
>>> On Thu, Oct 21, 2010 at 7:06 AM, Ben Hoskings <[email protected]> wrote:
>>>
>>>> 2010/10/21 Bodaniel Jeanes <[email protected]>
>>>>
>>>> Ben,
>>>>>
>>>>> I assume you are doing has_many :boards, through => :memberships?
>>>>>
>>>>> Isn't this just @user.memberships? Or are users associated with boards
>>>>> in a different wy?
>>>>>
>>>>
>>>> Yep, User -> Membership and Board -> Membership are both standard
>>>> has_many relationships.
>>>>
>>>> So, current_user.memberships is all this user's memberships. I'm after
>>>> all the memberships on all the boards this user is a member of. That is, 
>>>> for
>>>> every membership that Membership.where(:user_id => current_user.id)
>>>> returns, I want to also include every other membership to the corresponding
>>>> board.
>>>>
>>>> (The purpose of this is a readable_by filter for Membership: it's all
>>>> the memberships that this user can know exist. That is, a given user can 
>>>> see
>>>> all the memberships on all the boards they're a member of.)
>>>>
>>>> (If there's a better way to do what I'm trying to do then I'd be keen to
>>>> hear that too.)
>>>>
>>>> —ben_h
>>>>
>>>>  --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Ruby or Rails Oceania" group.
>>>> To post to this group, send email to [email protected].
>>>> To unsubscribe from this group, send email to
>>>> [email protected]<rails-oceania%[email protected]>
>>>> .
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/rails-oceania?hl=en.
>>>>
>>>
>>>  --
>>> You received this message because you are subscribed to the Google Groups
>>> "Ruby or Rails Oceania" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected]<rails-oceania%[email protected]>
>>> .
>>> For more options, visit this group at
>>> http://groups.google.com/group/rails-oceania?hl=en.
>>>
>>
>>
>  --
> You received this message because you are subscribed to the Google Groups
> "Ruby or Rails Oceania" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<rails-oceania%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/rails-oceania?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
or Rails Oceania" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.

Reply via email to