If I have a Person model, and every person has a parent, that is in fact another person, can I still use the belongs_to and has_many methods? A simple database migration might look like this t.column :name, :string t.column :parent, :integer So every person only has a name and some people have parents. I have written some methods that find a person''s parent and children: class Person < ActiveRecord::Base def find_children Person.find_all_by_parent(self.id) end def find_parent Person.find_by_id(self.parent) end end These seem to work fine, but I was wondering if there was a neater way to do this? Thanks, DAZ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
if you don''t need multiple parents per child (i.e, a parent has a lot of kids, and each kid has one parent) then this should be perfect for you: http://api.rubyonrails.com/classes/ActiveRecord/Acts/Tree/ClassMethods.html#M000662 no need for two tables; it is self-referential in the sense that it points back to the same table (=model) via a parent_id column. some other good pointers while you''re at it: acts_as_tree (the above link) acts_as_nested_set (for getting a whole bunch of kids in one query, instead of recursive calls) this is a real self-referential model usage; http://blog.hasmanythrough.com/2006/4/21/self-referential-through : just general knowledge, the best place to use this is for friendships (where X can be a friend of Y, without Y being a friend of X, as they are all "friends") anyway... i hope the above helps. good luck digging :) -- 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 -~----------~----~----~----~------~----~------~--~---
Thanks for this Shai, acts_as_tree is exactly what I was looking for. I might also use acts_as_list also. Thanks for the other links too ... I''m sure they will make some interesting and useful reading. DAZ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---