# Using includes twice

**URL:** <https://www.ruby-forum.com/t/using-includes-twice/130149>\
**Category:** Rails\
**Created:** [February 25, 2008, 9:14pm UTC](https://www.ruby-forum.com/t/using-includes-twice/130149 "2008-02-25T21:14:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 25, 2008, 9:14pm UTC](https://www.ruby-forum.com/t/using-includes-twice/130149/1 "2008-02-25T21:14:57Z")

</div>

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
