title: How to Implement a Friendship Model in Rails 3 for a Social Networking Application?
body: <p>I'm currently working on a small social networking application and right now I'm trying to create a model that represents <em>friendships</em> between <em>users</em>. This is what I came up with so far:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
  
    # ...
    has_many :friendships
    has_many :friends, :through =&gt; :friendships
  
  end
  
  class Friendship &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :friend, :class_name =&gt; 'User'
  end
  </code></pre>
  
  <p>My <em>friendship</em> model has a field <em>confirmed</em> as boolean which I'd like to use
  to define a friendship as pending or confirmed.</p>
  
  <p>How can I access all pending request for a certain user? Can I somehow define
  this using Rails' <em>scope</em> method? Something like</p>
  
  <pre><code>current_user.friendships.requests # =&gt; [Friendship, Friendship, ...]
  </code></pre>
  
  <p>would be great.</p>
  
  <p>How can I make this association bidirectional? Do I simply add another
  friendship once one has confirmed a friend request such that my friendship
  table would look similar to this:</p>
  
  <pre><code>| user_id | friend_id | confirmed |
  -----------------------------------
  | 1       | 2         | true      |
  | 2       | 1         | true      |
  </code></pre>