# Better nested set and :through

**URL:** <https://www.ruby-forum.com/t/better-nested-set-and-through/93220>\
**Category:** Rails\
**Created:** [April 29, 2007, 2:54pm UTC](https://www.ruby-forum.com/t/better-nested-set-and-through/93220 "2007-04-29T14:54:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vincent\_AE\_Scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6bbea6/32.png) [@Vincent\_AE\_Scott](https://www.ruby-forum.com/u/Vincent_AE_Scott)\
**Post date:** [April 29, 2007, 2:54pm UTC](https://www.ruby-forum.com/t/better-nested-set-and-through/93220/1 "2007-04-29T14:54:07Z")

</div>

Hello all,

It’s been a while since i’ve been doing any rails development, so i’m a  
bit rusty. Having found this:  
[http://opensource.symetrie.com/api/better\_nested\_set/](http://opensource.symetrie.com/api/better_nested_set/)

it looks really interesting and useful for solving something i’m trying  
to get working.

What i have (essentially) is the following models:

class Group \< ActiveRecord::Base  
acts\_as\_nested\_set  
has\_many :memberships  
has\_many :users, :through =\> :memberships  
belongs\_to :owner, :class\_name =\> “User”  
end

class Membership \< ActiveRecord::Base  
belongs\_to :group  
belongs\_to :user  
end

class User \< ActiveRecord::Base  
has\_many :memberships  
has\_many :groups, :through =\> :memberships

end

Now then, what i’d like to know is how do i go about finding all the  
Users contained within a nested set?

for example:

admins = Group.find\_by\_name “admins”  
admins.users # gives all users in the admins group.

and

admins.all\_children # gives me all the groups

I thought the following methods sounded promising, but i get  
NoMethodError’s from them:

admins.children.find\_all\_in\_users  
admins.children.find\_in\_users

Perhaps i’m going about this the wrong way or i’m missing a more obvious  
solution. Can anyone shed some light on it?

Cheers,  
-vince

---

<div class="post-metadata">

**Author:** ![Vincent\_AE\_Scott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6bbea6/32.png) [@Vincent\_AE\_Scott](https://www.ruby-forum.com/u/Vincent_AE_Scott)\
**Post date:** [April 29, 2007, 11:55pm UTC](https://www.ruby-forum.com/t/better-nested-set-and-through/93220/2 "2007-04-29T23:55:43Z")

</div>

Hi,

Le 29 avr. 07, à 14:54, Vincent AE Scott a écrit :

> ```
> acts_as_nested_set
> 
> ```
> 
> class User \< ActiveRecord::Base  
> admins = Group.find\_by\_name “admins”  
> admins.children.find\_in\_users
> 
> Perhaps i’m going about this the wrong way or i’m missing a more  
> obvious  
> solution. Can anyone shed some light on it?

I think the problem you raise is more related to find and :through than  
to nested sets.  
You should have a look on :include in find :

admins = Group.find\_by\_name ‘admins’, :include =\> [{‘memberships’ =\>  
‘users’}]

then simply grab your users with

users = admins.inject([]){|u, a| u \<\< a.users}.uniq

## Jean-Christophe M.

[symetrie.com](http://symetrie.com)

Better Nested Set for rails:  
[http://opensource.symetrie.com/trac/better\_nested\_set](http://opensource.symetrie.com/trac/better_nested_set)
