title: How to model a mutual friendship in Rails
body: <p>I know this question has been asked before on Stack Overflow, but the answers aren't doing it for me in ways I can explain. My general approach was inspired by <a href="http://railscasts.com/episodes/163-self-referential-association?view=asciicast" rel="noreferrer">this</a> tutorial.</p>
  
  <p>What I'm trying to do, is create a really simple model for friending users that creates an equivalent friendship on both ends with a single record.</p>
  
  <p>At the db level, I just have a 'friendships' table that just has a user_id, a friend_id, and an is_pending boolean column.</p>
  
  <p>In user.rb I've defined the relationship as:</p>
  
  <pre><code>has_many :friendships
  has_many :friends, through: :friendships
  </code></pre>
  
  <p>In friendship.rb, I've defined the relationship as:</p>
  
  <pre><code>belongs_to :user
  belongs_to :friend, :class_name =&gt; 'User'
  </code></pre>
  
  <p>If I add a friendship, I can access as follows:</p>
  
  <pre><code>&gt; a = User.first
  &gt; b = User.last
  &gt; Friendship.new(a.id, b.id)
  &gt; a.friends
  =&gt; #&lt;User b&gt;
  </code></pre>
  
  <p>That's perfect, but what I want is to also be able to go in the other direction like so:</p>
  
  <pre><code>&gt; b.friends
  </code></pre>
  
  <p>Unfortunately, with the relationship defined as it is, I get an empty collection. The SQL that runs shows that it's searching for user_id = b.id. How do I specify that it should also search for friend_id = b.id?</p>