Im trying to figure out how to do a mutual 2-way relationship, that is:
user_id friend_id
1 2
2 1
In above user 1 and user 2 would be friends if both user_id = 1 has friend_id = 2 and friend_id = 2 has user_id = 2 as there friend in a table. How to count all the 2-way mutual relations in ActiveRecord?
What you’re looking for is a
has_and_belongs_to_manyrelationship:Example is from §4.4.2.1.