Hello,
Is there a way to handle includes twice?
Basic models, Group and User.
Group has many users, users belong to group.
Users have a ‘role’, which can be manager/developer and stuff like
that.
Say I want to find all groups that have a manager in them, including
all users.
Group.find(:all, :include => :users, :conditions => [“users.role = ?”,
‘Manager’]) won’t do the trick, since it will indeed find all groups
that have managers, but it fails to include all users.
Of course this is to be expected because of the conditions, but I
think there must be some way to include users twice in the query, once
for the conditions, and once as the ‘real output’
Ofcourse I can write it like this:
managed_groups_including_users =
User.find_all_by_role(‘Manager’, :include => {:group
=> :users}).map(&:group).uniq
But that involves 2 operations in memory (mapping and uniq’ing) which
isn’t nice to do on large datasets.
Also since it’s the groups I’m after, it doesn’t feel right to start
asking the User model.
Any thoughts?
Thanks,
Mathijs