Simple caching

I think you will need to use acts_as_tree (or some other method like
nested_set?) rather than using a circular belongs_to in the same class.
Try this:

class Agent < ActiveRecord::Base
  acts_as_tree :foreign_key => "manager_id"

  def manager_full_name
  manager = self.parent
  manager_full_name = ''
  manager_full_name = manager.full_name unless manager == nil


Note: the default foreign_key column to get the parent is parent_id but
we can override that since you called it manager_id. Also, self.agent
in your example becomes self.parent.

There were a couple of typos in your example that you are probably
aware of but here they are anyway: Nil should be lowercase nil and <%=
agent.manager_full_name => should be <%= @agent.manager_full_name %>.

To retrieve the agent AND the parent from the database with a single
query use the :include parameter as follows: @agent = Agent.find(2),
:include => :parent). This will retrieve the agent row for id 2 along
with it's parent at the same time.

I don't have a good UI solution for displaying these in heirarchical
order. Maybe someone else has tried it.


But I don't know how you can add it to the 'list'
action when you do a Agent.find(:all). How can you get a list of agents
and at the same time load the parent of each agent.

Just do Agent.find(:all, :include => :parent).