Better nested set and :through

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/

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

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

Better Nested Set for rails:
http://opensource.symetrie.com/trac/better_nested_set

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs