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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jean-Christophe Michel
2007-Apr-29 21:54 UTC
[Betternestedset-talk] [Rails] better nested set and :through
Hi, Le 29 avr. 07, ? 14:54, Vincent AE Scott a ?crit :> 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 -- symetrie.com Better Nested Set for rails: http://opensource.symetrie.com/trac/better_nested_set