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.
on 2008-11-11 10:37
on 2008-11-11 10:56
Make two entries, one for each "direction" A is friend of B B is friend of A