Association of table to itself

I’m curious what would be the best way to represent people and whether
the
person has someone marked as their friend. Here are my thoughts so far:

People table

  • id
  • name

Relationships table

  • person_id
  • friend_id

Now my problem is I don’t know how to set up these associations (if this
is
even correct). Friend_id would be an id to someone else from the people
table.

I’m curious what would be the best way to represent people and whether
the
person has someone marked as their friend. Here are my thoughts so far:

People table

  • id
  • name

Relationships table

  • person_id
  • friend_id

Now my problem is I don’t know how to set up these associations (if this
is
even correct). Friend_id would be an id to someone else from the people
table.

Davy C. wrote:

I’m curious what would be the best way to represent people and whether the
person has someone marked as their friend. Here are my thoughts so far:

People table

  • id
  • name

Relationships table

  • person_id
  • friend_id

Now my problem is I don’t know how to set up these associations (if this is
even correct). Friend_id would be an id to someone else from the people table.

You can do this with a self-referential has_many :through association.

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


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

You should check out the rails book

Section 15.3

Single table inheritance


From: [email protected]
[mailto:[email protected]] On Behalf Of Davy C.
Sent: Sunday, May 28, 2006 10:11 AM
To: [email protected]
Subject: [Rails] Association of table to itself

I’m curious what would be the best way to represent people and whether
the
person has someone marked as their friend. Here are my thoughts so far:

People table

  • id
  • name

Relationships table

  • person_id
  • friend_id

Now my problem is I don’t know how to set up these associations (if this
is
even correct). Friend_id would be an id to someone else from the people
table.