Thanks for your reply.

I decided to go with your second suggestion
(Group.one_to_many :memberships, :eager_graph=>:user),
since it might also be a lot clearer what's going on in that case.
One thing though.
You mentioned I can filter on memberships and reference the user table
(since it is eagerly included).
Is there a nice way to use dataset methods I already have on the user class?

So something like
g.memberships_dataset.filter(:user => User.recently_active)

This way I won't have to repeat what recently_active means on multiple models.

Thanks,
Mathijs


2009/3/12 Jeremy Evans <[email protected]>:
>
> On Mar 11, 11:06 am, Mathijs <[email protected]> wrote:
>> Hi,
>>
>> I tried this in activerecord and it was a real problem to get it
>> right.
>> I think sequel will make this easier.
>>
>> simple problem:
>> Group habtm User
>> User habtm Group
>>
>> join model is named Membership.
>>
>> say I gave group g.
>> g.users should return a list of users for that group (works out of the
>> box),
>> but I want the User instances to know/cache the membership record that
>> was used to get to them from g.
>> So I can get users to display stuff about their membership to g (since
>> when, status) without having to do a new query for them.
>>
>> Now, I know I can also fetch g's memberships and eager load the user,
>> but I would like this the other way around, so I can still use filters
>> and such on users.
>>
>> From an OO-perspective it's a bit odd, but I would like all user
>> instances (fetched through this association) to have a
>> 'membership' (singular) attribute that caches the membership used to
>> get to self. This might be a bit strange since users already have
>> 'memberships' (plural).
>>
>> Is something like this possible using sequel?
>> Or would it be a dirty hack?
>
> You can get the information you want, but not easily in the format you
> want.  You could use a :select option for the association and add the
> columns in the join table.  Something like:
>
>  Group.many_to_many :users, :select=>
> [:users.*, :memberships__column1, :memberships__column2]
>
> The extra columns will appear as part of the user objects returned.
> If you want to have separate membership objects, you can do something
> like:
>
>  Group.one_to_many :memberships, :eager_graph=>:user
>
> That'll eager load the user when you request the memberships, using a
> join so you can still use filters referencing columns in the users
> table. So you can access the user at membership.user without a query.
>
> To really get what you want, you'll have to use the :dataset and
> maybe :eager_loader options and create a custom association.  If I did
> that, I'd just store the cached membership in an instance variable and
> add an instance method to user.  I'd probably use one of the 2
> approaches above instead, though.  Look at the advanced associations
> rdoc page for examples of custom associations if you choose the hard
> path.
>
> Jeremy
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sequel-talk" 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/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to