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.
C62eb10afff640cb0fa31cfaa07f6715?d=identicon&s=25 Davy Campano (Guest)
on 2006-05-28 18: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.
C62eb10afff640cb0fa31cfaa07f6715?d=identicon&s=25 Davy Campano (Guest)
on 2006-05-28 19: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.
E6d130d35ecbda878f38318b98d5043c?d=identicon&s=25 Clayton Cottingham (Guest)
on 2006-05-28 19:23
(Received via mailing list)
You should check out the rails book



Section 15.3



Single table inheritance



  _____

From: rails-bounces@lists.rubyonrails.org
[mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Davy Campano
Sent: Sunday, May 28, 2006 10:11 AM
To: rails@lists.rubyonrails.org
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.
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2006-05-28 19:50
Davy Campano 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 Susser
http://blog.hasmanythrough.com
This topic is locked and can not be replied to.