title: Linking Two Models Together in Ruby on Rails
body: <p>I've been stuck on this all day.  I have a setup like the one below.  I'm trying to define friends using the group_memberships association.    </p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :group_memberships
    has_many :groups, :through =&gt; :group_memberships
    has_many :friends # what goes here? &lt;&lt;
  end
  
  class GroupMembership &lt; ActiveRecord::Base
    belongs_to :user
    belongs_to :role
    belongs_to :group
  end
  
  class Role &lt; ActiveRecord::Base
    has_many :group_memberships
  end
  
  class Group &lt; ActiveRecord::Base
    has_many :group_memberships
    has_many :users, :through &gt; :group_memberships
  end
  </code></pre>
  
  <p>I'd like to do this without creating a join table for friends, unless it's completely crazy to do it without.</p>
  
  <p>The group_membership table contains user_id and group_id linking one user to one group. </p>
  
  <p>I'd trying to get</p>
  
  <pre><code>@user.friends
  </code></pre>
  
  <p>to return users with common group_memberships using the group_id.</p>
  
  <pre><code>has_many :friends, :through =&gt; :group_memberships, :source =&gt; :group
  </code></pre>
  
  <p>Nothing I've tried works, but I'll chalk that up to my complete misunderstanding of the above code.</p>