Hi,
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/
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?
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