One table, many references

I’m a total newby to Ruby and jumped into an application that quickly
went
over my head. My question relates to how I can setup the models in my
app
so that I can reference the same database table using different names.
Here’s how my database is setup:

table requests
id
title
sponsor_id
initiator_id
implementor_id

table employees
id
fname
lname

The sponsor_id, initiator_id, and implementor_id fields are all intended
to
reference an row in the employees table. I’m confused as to how I make
rails understand that these foreign keys all reference the same table.
Can
anyone help?

View this message in context:
http://www.nabble.com/One-table%2C-many-references-tf2255999.html#a6257463
Sent from the RubyOnRails Users forum at Nabble.com.

Asterix wrote:

I’m a total newby to Ruby and jumped into an application that quickly
went over my head. My question relates to how I can setup the models in my
app so that I can reference the same database table using different names.
Here’s how my database is setup:

table requests
id
title
sponsor_id
initiator_id
implementor_id

table employees
id
fname
lname

The sponsor_id, initiator_id, and implementor_id fields are all intended
to reference an row in the employees table. I’m confused as to how I make
rails understand that these foreign keys all reference the same table.
Can anyone help?

belongs_to :sponsor, :class_name => “Employee”, :foreign_key =>
“sponsor_id”
belongs_to :initiator, :class_name => “Employee”, :foreign_key =>
“initiator_id”
belongs_to :implementor, :class_name => “Employee”, :foreign_key =>
“implementor_id”

You should read the docs on belongs_to. It’s all right there.


Josh S.
http://blog.hasmanythrough.com