better nested set and :through


It's been a while since i've been doing any rails development, so i'm a
bit rusty. Having found this:

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
        has_many :memberships
        has_many :users, :through => :memberships
        belongs_to :owner, :class_name => "User"

class Membership < ActiveRecord::Base
        belongs_to :group
        belongs_to :user

class User < ActiveRecord::Base
        has_many :memberships
        has_many :groups, :through => :memberships


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.


admins.all_children # gives me all the groups

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


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 Michel