Paul Barry
2006-Jan-12 06:14 UTC
[Rails] How do you create a tree strucutre with ActiveRecord
I want to build an application that has the concept of administrative domains. What I mean by this is that administrators have access to different data, based on what domains they are a member of. The domain strucutre is hierarchical. Here is an example: - MLB - AL - East - Yankees - Red Sox ... + Central + West - NL + East + Central + West Now image I could put users at any level of the domain, possibly even at multple levels. If the user has administrative privleges, the user would be able to edit the user accounts for other users in there domain, but not users in other domains. So, Derek Jeter would be a user in the Yankees domain, George Steinbrenner and admin user in the Yankees domain, David Ortiz a user in the Red Sox domain, and Bud Selig an admin user in the MLB domain. Since Ortiz and Jeter are just non-admin users, they can''t edit any other user accounts. Since steinbrenner is ad admin user in the Yankees domain, he can edit Jeter''s account, but not Ortiz''s, because Ortiz is not in Steinbrenner''s domain. Bud Selig could edit both Jeter and Ortiz''s account, because they are in sub-domains of his domain. The database structure would have to look something like this: Table: Domains +----+-----------+---------+ | id | parent_id | name | +----+-----------+---------+ | 1 | NULL | MLB | | 2 | 1 | AL | | 3 | 2 | East | | 4 | 3 | Yankees | | 5 | 3 | Red Sox | +----+-----------+---------+ parent_id is a FK to another domain id. A NULL parent_id indicates it is a top-level domain. So there would be a users and a domain_users table, and Domain model object would have a has_many_and_belongs_to_many relationship with users. But the problem is that would only allow me to query what users are directly in a given domain. In order to determine what users are in a given domain, you have to include the child domains has well. So what users are in domain MLB? The query would be: SELECT * from domain_users where domain_id IN (1,2,3,4,5) Then for steinbrenner it would be: SELECT * from domain_users where domain_id IN (3) But how do you write a query to know what child, grandchild, etc domains to use? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060112/0b1e1e32/attachment.html
M Daggett
2006-Jan-12 06:23 UTC
[Rails] How do you create a tree strucutre with ActiveRecord
Hi the easiest way is ...drum roll ...... acts_as_tree: http://api.rubyonrails.com/classes/ActiveRecord/Acts/Tree/ClassMethods.html hope that helps. Mark On 1/11/06, Paul Barry <mail@paulbarry.com> wrote:> I want to build an application that has the concept of administrative > domains. What I mean by this is that administrators have access to > different data, based on what domains they are a member of. The domain > strucutre is hierarchical. Here is an example: > > - MLB > - AL > - East > - Yankees > - Red Sox > ... > + Central > + West > - NL > + East > + Central > + West > > Now image I could put users at any level of the domain, possibly even at > multple levels. If the user has administrative privleges, the user would be > able to edit the user accounts for other users in there domain, but not > users in other domains. So, Derek Jeter would be a user in the Yankees > domain, George Steinbrenner and admin user in the Yankees domain, David > Ortiz a user in the Red Sox domain, and Bud Selig an admin user in the MLB > domain. Since Ortiz and Jeter are just non-admin users, they can''t edit any > other user accounts. Since steinbrenner is ad admin user in the Yankees > domain, he can edit Jeter''s account, but not Ortiz''s, because Ortiz is not > in Steinbrenner''s domain. Bud Selig could edit both Jeter and Ortiz''s > account, because they are in sub-domains of his domain. > > The database structure would have to look something like this: > > Table: Domains > +----+-----------+---------+ > | id | parent_id | name | > +----+-----------+---------+ > | 1 | NULL | MLB | > | 2 | 1 | AL | > | 3 | 2 | East | > | 4 | 3 | Yankees | > | 5 | 3 | Red Sox | > +----+-----------+---------+ > > parent_id is a FK to another domain id. A NULL parent_id indicates it is a > top-level domain. So there would be a users and a domain_users table, and > Domain model object would have a has_many_and_belongs_to_many relationship > with users. But the problem is that would only allow me to query what users > are directly in a given domain. In order to determine what users are in a > given domain, you have to include the child domains has well. So what users > are in domain MLB? The query would be: > > SELECT * from domain_users where domain_id IN (1,2,3,4,5) > > Then for steinbrenner it would be: > > SELECT * from domain_users where domain_id IN (3) > > But how do you write a query to know what child, grandchild, etc domains to > use? > > > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >-- -------------------------------------------------------------------- I am Mark Daggett and I approve this message.
Kevin Olbrich
2006-Jan-12 06:27 UTC
[Rails] Re: How do you create a tree strucutre with ActiveRecord
Sounds like you are looking for ''acts_as_tree'' Basically you define a model table like this.. id parent_id other_stuff models_count -- --------- ----------- ------------ then in your model class Model < AR::Base acts_as_tree :order=>"other_stuff", :counter_cache=>true end then you can do all sorts of fun stuff like model.children model.parents model.ancestors model.siblings The counter cache will keep the ''models_count'' column up to date with the number of children, so you only need one query for that information. _Kevin -- Posted via http://www.ruby-forum.com/.
Ken Kousen
2006-Jan-12 14:59 UTC
[Rails] How do you create a tree strucutre with ActiveRecord
Actually, the biggest problem you have is that you listed the Yankees ahead of the Red Sox. :-) Ken -- Kenneth A. Kousen, Ph.D. President Kousen IT, Inc. <http://www.kousenit.com> http://www.kousenit.com <mailto:ken.kousen@kousenit.com> ken.kousen@kousenit.com _____ From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Paul Barry Sent: Thursday, January 12, 2006 1:14 AM To: rails@lists.rubyonrails.org Subject: [Rails] How do you create a tree strucutre with ActiveRecord I want to build an application that has the concept of administrative domains. What I mean by this is that administrators have access to different data, based on what domains they are a member of. The domain strucutre is hierarchical. Here is an example: - MLB - AL - East - Yankees - Red Sox ... + Central + West - NL + East + Central + West Now image I could put users at any level of the domain, possibly even at multple levels. If the user has administrative privleges, the user would be able to edit the user accounts for other users in there domain, but not users in other domains. So, Derek Jeter would be a user in the Yankees domain, George Steinbrenner and admin user in the Yankees domain, David Ortiz a user in the Red Sox domain, and Bud Selig an admin user in the MLB domain. Since Ortiz and Jeter are just non-admin users, they can''t edit any other user accounts. Since steinbrenner is ad admin user in the Yankees domain, he can edit Jeter''s account, but not Ortiz''s, because Ortiz is not in Steinbrenner''s domain. Bud Selig could edit both Jeter and Ortiz''s account, because they are in sub-domains of his domain. The database structure would have to look something like this: Table: Domains +----+-----------+---------+ | id | parent_id | name | +----+-----------+---------+ | 1 | NULL | MLB | | 2 | 1 | AL | | 3 | 2 | East | | 4 | 3 | Yankees | | 5 | 3 | Red Sox | +----+-----------+---------+ parent_id is a FK to another domain id. A NULL parent_id indicates it is a top-level domain. So there would be a users and a domain_users table, and Domain model object would have a has_many_and_belongs_to_many relationship with users. But the problem is that would only allow me to query what users are directly in a given domain. In order to determine what users are in a given domain, you have to include the child domains has well. So what users are in domain MLB? The query would be: SELECT * from domain_users where domain_id IN (1,2,3,4,5) Then for steinbrenner it would be: SELECT * from domain_users where domain_id IN (3) But how do you write a query to know what child, grandchild, etc domains to use? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060112/8d3f68d7/attachment.html
Peter Ertl
2006-Jan-12 15:52 UTC
[Rails] How do you create a tree strucutre with ActiveRecord
acts_as_tree in ActiveRecord works very well for me read "Agile Web Development in Rails" for more information> --- Urspr?ngliche Nachricht --- > Von: "Ken Kousen" <ken.kousen@kousenit.com> > An: <rails@lists.rubyonrails.org> > Betreff: RE: [Rails] How do you create a tree strucutre with ActiveRecord > Datum: Thu, 12 Jan 2006 09:59:03 -0500 > > Actually, the biggest problem you have is that you listed the Yankees > ahead > of the Red Sox. :-) > > > > Ken > > > > -- > > Kenneth A. Kousen, Ph.D. > > President > > Kousen IT, Inc. > > <http://www.kousenit.com> http://www.kousenit.com > > <mailto:ken.kousen@kousenit.com> ken.kousen@kousenit.com > > _____ > > From: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Paul Barry > Sent: Thursday, January 12, 2006 1:14 AM > To: rails@lists.rubyonrails.org > Subject: [Rails] How do you create a tree strucutre with ActiveRecord > > > > I want to build an application that has the concept of administrative > domains. What I mean by this is that administrators have access to > different data, based on what domains they are a member of. The domain > strucutre is hierarchical. Here is an example: > > - MLB > - AL > - East > - Yankees > - Red Sox > ... > + Central > + West > - NL > + East > + Central > + West > > Now image I could put users at any level of the domain, possibly even at > multple levels. If the user has administrative privleges, the user would > be > able to edit the user accounts for other users in there domain, but not > users in other domains. So, Derek Jeter would be a user in the Yankees > domain, George Steinbrenner and admin user in the Yankees domain, David > Ortiz a user in the Red Sox domain, and Bud Selig an admin user in the MLB > domain. Since Ortiz and Jeter are just non-admin users, they can''t edit > any > other user accounts. Since steinbrenner is ad admin user in the Yankees > domain, he can edit Jeter''s account, but not Ortiz''s, because Ortiz is not > in Steinbrenner''s domain. Bud Selig could edit both Jeter and Ortiz''s > account, because they are in sub-domains of his domain. > > The database structure would have to look something like this: > > Table: Domains > +----+-----------+---------+ > | id | parent_id | name | > +----+-----------+---------+ > | 1 | NULL | MLB | > | 2 | 1 | AL | > | 3 | 2 | East | > | 4 | 3 | Yankees | > | 5 | 3 | Red Sox | > +----+-----------+---------+ > > parent_id is a FK to another domain id. A NULL parent_id indicates it is > a > top-level domain. So there would be a users and a domain_users table, and > Domain model object would have a has_many_and_belongs_to_many relationship > with users. But the problem is that would only allow me to query what > users > are directly in a given domain. In order to determine what users are in a > given domain, you have to include the child domains has well. So what > users > are in domain MLB? The query would be: > > SELECT * from domain_users where domain_id IN (1,2,3,4,5) > > Then for steinbrenner it would be: > > SELECT * from domain_users where domain_id IN (3) > > But how do you write a query to know what child, grandchild, etc domains > to > use? > > > > > >