Association of table to itself


#1

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.


#2

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.


#3

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


#4

You should check out the rails book

Section 15.3

Single table inheritance


From: removed_email_address@domain.invalid
[mailto:removed_email_address@domain.invalid] On Behalf Of Davy C.
Sent: Sunday, May 28, 2006 10:11 AM
To: removed_email_address@domain.invalid
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.