# Possibility of ActiveRecord in combining two same tables

**URL:** <https://www.ruby-forum.com/t/possibility-of-activerecord-in-combining-two-same-tables/153032>\
**Category:** Rails\
**Created:** [November 11, 2008, 10:37am UTC](https://www.ruby-forum.com/t/possibility-of-activerecord-in-combining-two-same-tables/153032 "2008-11-11T10:37:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![parkurm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/90db22/32.png) [@parkurm](https://www.ruby-forum.com/u/parkurm)\
**Post date:** [November 11, 2008, 10:37am UTC](https://www.ruby-forum.com/t/possibility-of-activerecord-in-combining-two-same-tables/153032/1 "2008-11-11T10:37:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![parkurm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/90db22/32.png) [@parkurm](https://www.ruby-forum.com/u/parkurm)\
**Post date:** [November 11, 2008, 10:56am UTC](https://www.ruby-forum.com/t/possibility-of-activerecord-in-combining-two-same-tables/153032/2 "2008-11-11T10:56:38Z")

</div>

Make two entries, one for each “direction”  
A is friend of B  
B is friend of A
