Consider a tree. Not a perfect example since every branch only has one parent...but you get the idea. What I want is the following: Branch.children Branch.parents I think the problem is not being able to change the method name without changing the attribute it points to. Here is my idea so far. Thanx for looking! class Branch < ActiveRecord::Base has_many :parents, :through => :relations has_many :children, :through => :relations end class Relation < ActiveRecord::Base belongs_to :branch, :foreign_key => "parent_id" belongs_to :branch, :foreign_key => "child_id" end ---------DB------- table Relations parent_id int child_id int foo --- bar --- --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You''re going to have to be very, very careful to not create loops (eg situations where something is a child of itself) if you''re going after a tree structure. That''s the sort of data integrity thing that I would shove right down to the database layer as triggers on your appropriate table. The schema here (from my old perl days) does that: http://search.cpan.org/src/CRAKRJACK/Schema-RDBMS-AUS-0.03/schema/ In postgres: http://search.cpan.org/src/CRAKRJACK/Schema-RDBMS-AUS-0.03/schema/Pg/0.01/216_user_membership_insert.sql In MySQL: http://search.cpan.org/src/CRAKRJACK/Schema-RDBMS-AUS-0.03/schema/mysql/0.01/216_user_membership_insert.sql Hopefully you''ll find something there you can use. Cheers, Tyler Josh <josh.m.sharpe-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Consider a tree. Not a perfect example since every branch only has > one parent...but you get the idea. > > What I want is the following: > > Branch.children > Branch.parents > > I think the problem is not being able to change the method name > without changing the attribute it points to. Here is my idea so far. > > Thanx for looking! > > class Branch < ActiveRecord::Base > has_many :parents, :through => :relations > has_many :children, :through => :relations > end > > class Relation < ActiveRecord::Base > belongs_to :branch, :foreign_key => "parent_id" > belongs_to :branch, :foreign_key => "child_id" > end > > ---------DB------- > table Relations > parent_id int > child_id int > foo --- > bar --- > > > >-- --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hmm, check out plugins like acts_as_graph: http://blog.tammersaleh.com/pages/acts_as_graph Jason On 4/12/07, Josh <josh.m.sharpe-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Consider a tree. Not a perfect example since every branch only has > one parent...but you get the idea. > > What I want is the following: > > Branch.children > Branch.parents > > I think the problem is not being able to change the method name > without changing the attribute it points to. Here is my idea so far. > > Thanx for looking! > > class Branch < ActiveRecord::Base > has_many :parents, :through => :relations > has_many :children, :through => :relations > end > > class Relation < ActiveRecord::Base > belongs_to :branch, :foreign_key => "parent_id" > belongs_to :branch, :foreign_key => "child_id" > end > > ---------DB------- > table Relations > parent_id int > child_id int > foo --- > bar --- > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---