Self referential Models

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

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