Forum: Ruby on Rails Possibility of ActiveRecord in combining two same tables

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.
parkurm (Guest)
on 2008-11-11 11:37
(Received via mailing list)
The title may be somehow puzzling. Suppose an Social Networking Site,
we have one table to keep each person's id and name. Then we need the
function to have friends, so we'll have to create a table to store the
information that two persons are friends. So this table will contain
two columns, both of them referring to one person. But the problem
here is there's no order here, which means one pair of friends are
stored in just one row, (Person A - Person B or Person B - Person A,
whichever comes into database first). In this scheme, ActiveRecord
will have trouble if I use "has_and_belongs_to_many", since if I call
person.friends (suppose this is name of the table to store friend
information), ActiveRecord will only look up in one column of friends
table, either first or second, depending the name of column assigned.
So could anybody tell me how to deal with this kind of problem? I
searched this group, and found a similar topic, "ActiveRecord:
Reference same table twice", but this one there's an order in two
columns so there'll be no problem.
Thorsten M. (Guest)
on 2008-11-11 11:56
(Received via mailing list)
Make two entries, one for each "direction"
A is friend of B
B is friend of A
This topic is locked and can not be replied to.