Forum: Ruby on Rails Association of table to itself

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Davy C. (Guest)
on 2006-05-28 20:33
(Received via mailing list)
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. (Guest)
on 2006-05-28 21:13
(Received via mailing list)
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.
Clayton C. (Guest)
on 2006-05-28 21:23
(Received via mailing list)
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.
Josh S. (Guest)
on 2006-05-28 21:50
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...

--
Josh S.
http://blog.hasmanythrough.com
This topic is locked and can not be replied to.