On 5/15/07, David Wolever <[EMAIL PROTECTED]> wrote:
>
> Hey,
> I've got two objects which relate to each other with
> has_and_belongs_to_many, Person and Group.
> I am trying to create a query that will map data between a third
> class, Time (Person has_many(times, of_kind="Time")), and Group.
> Right now I'm using a query similar to:
>
> secondary_table = Person.mapper.properties["groups"].secondary
> query = select([Times.c.time],
> and_(TimeAvailable.c.person_id == Person.c.id,
> secondary_table.c.people_id == Person.c.id,
> secondary_table.c.groups_id == group_id_to_find)
You'll have to find the exact incantation for your particular set of
classes, but this is what join_to and join_via is for.
To join Person to Group, you'd use something like
Person.join_to('groups') in your "WHERE clause". This will add the
necessary join and where clauses such that only the groups that the
user belongs to are put next to the user. This totally removes the
need to know about the secondary table.
To get from Times to Group, it may be Times.join_via('person', 'groups').
Neil
--
Neil Blakey-Milner
http://nxsy.org/
[EMAIL PROTECTED]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"SQLElixir" 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/sqlelixir?hl=en
-~----------~----~----~----~------~----~------~--~---