Has_one self reference

Imagine the following situation:

model Person, with the default name, address, location data.
Person has either one or no superior which in turn is also a Person
model instance.

I would expect something like
has_one :superior, :class_name => “Person”

but where would I place the belongs_to line that goes with it and what
arguments do I need to supply? And what kind of foreign keys do I need
to specify in my migration?

I would like for the following to work:

bossman = Person.new :name => “Mr. Bossman”
person1 = Person.new :name => “John”
person2 = Person.new :name => “Jane”

person1.superior = bossman
person2.superior = bossman

person1.superior.name # => “Mr. Bossman”

I wonder what kind of columns I need for this in my Persons table
manually specified and which columns are virtual. I experimented a bit
already and got something quite similar, but for some reason I was able
to modify the person1.superior_id independantly of the person1.superior
object. I’d rather have nothing to do with the _id and shield it from
being used directly.

On 18 Apr 2008, at 21:37, Chris D. wrote:

but where would I place the belongs_to line that goes with it and what
arguments do I need to supply? And what kind of foreign keys do I need
to specify in my migration?
I think you just need it to be belongs_to :superior and then it all
just works. The foreign key has to sit somewhere and it can’t be on
the superior (or else he’d only be able to be the superior of one
person).

Fred

Frederick C. wrote:

On 18 Apr 2008, at 21:37, Chris D. wrote:

but where would I place the belongs_to line that goes with it and what
arguments do I need to supply? And what kind of foreign keys do I need
to specify in my migration?
I think you just need it to be belongs_to :superior and then it all
just works. The foreign key has to sit somewhere and it can’t be on
the superior (or else he’d only be able to be the superior of one
person).

Fred

Thanks! From the docs I managed to do this:
belongs_to :superior, :class_name => ‘Person’, :foreign_key =>
‘superior_id’

It seems now through saving the object the superior_id and the actual
object are linked together as supposed to

Thanks!

This post has helped me so much so far but I am having trouble
accessing the data:
The Model is Employee and its table has a manager_id. A manager is
also an employee and the manager_id is populated with the employee_id.
Each employee has a name field.

In the Employee model I have:
belongs_to :manager, :class_name => “Employee”, :foreign_key =>
“manager_id”

When looping through Employees in my view I can <%= debug
@employee_object_ref.manager %> and I see the correct details.
But when accessing <%= @employee_object_ref.manager.name %>

The result is:

You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.name

I am bamboozled! What am I doing wrong?

Many thanks in advance.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs