Non-"table_id" foreign key

Okay folks, I’m feeling like a fool. I’m sure I’m not the first person
to run into this, but I can’t find the necessary info.

Basically I need to create two tables. The first to hold actual
content. And the second to link pairs of content records. For
example, a “persons” table and a “friends” table that pairs up two
person records. Obviously, I can’t have two fields in the friends
table named ‘person_id’. So how do I assign a field as a foreign key,
that isn’t named in the default format “table_id”?

your table friends:

has
left_person_id
right_person_id

so
class Friend

belongs_to :right_person, :class_name => ‘Person’, :foreign_key =>
‘right_person_id’
belongs_to :left_person, :class_name => ‘Person’, :foreign_key =>
‘left_person_id’

check out this article and the comments to it, someone did exactly
something like this…

http://blog.hasmanythrough.com/articles/2006/04/21/self-referential-through

igotimac, Thorsten L, & Bryan - Thank you all so much! You’ll ROCK!

will wrote:

Okay folks, I’m feeling like a fool. I’m sure I’m not the first person
to run into this, but I can’t find the necessary info.

Basically I need to create two tables. The first to hold actual
content. And the second to link pairs of content records. For
example, a “persons” table and a “friends” table that pairs up two
person records. Obviously, I can’t have two fields in the friends
table named ‘person_id’. So how do I assign a field as a foreign key,
that isn’t named in the default format “table_id”?

You could also use a has_and_belongs_to_many association if your mapping
table really just contains the 2 person ids.

Like igotimac implied in his comment, you can name your foreign keys
whatever you want, just reference them explicitly when you create your
association using the options macros for the association (foreign_key=>,
class_name=>, etc)

Here is a reference to has_and_belongs_to_many:
http://wiki.rubyonrails.org/rails/pages/has_and_belongs_to_many