title: has_many through with scope condition
body: <p>I am new to Rails and have a question on putting a condition on a <code>has_many :through</code> relationship. I want to have a <code>:joined_groups</code> relationship in the <code>FacebookUser</code> model. The joined_groups would be a through the GroupInvite table meeting the condition of <code>where(accepted: true)</code>. However I do not know how to accomplish this with Rails or if this is even the right way of doing it. I have the following models ...</p>
  
  <p><strong>Group.rb</strong></p>
  
  <pre><code>class Group &lt; ActiveRecord::Base
    attr_accessible :description, :name
  
    ...
  
    #Relationships
    has_many :group_invites, class_name: 'GroupInvite'
    has_many :facebook_users, class_name: 'FacebookUser', :through =&gt; :group_invites, :source =&gt; :facebook_user
  end
  </code></pre>
  
  <p><strong>GroupInvite.rb</strong></p>
  
  <pre><code>class GroupInvite &lt; ActiveRecord::Base
    attr_accessible :accepted, :facebook_user_uid, :group_id, :admin
  
    ...    
  
    #Relationships
    belongs_to :group
    belongs_to :facebook_user, :foreign_key =&gt; :facebook_user_uid, :primary_key =&gt; :facebook_user_uid
  
    #Scopes
    scope :pending, where(accepted: nil)
    scope :declined, where(accepted: false)
    scope :joined, where(accepted: true)
  end
  </code></pre>
  
  <p><strong>FacebookUser</strong></p>
  
  <pre><code>class FacebookUser &lt; ActiveRecord::Base
  
    #Attributes
    attr_accessible :first_name, :gender, :last_name, :uid
  
    ...
  
    has_many :group_invites, class_name: 'GroupInvite', :primary_key =&gt; :uid, :foreign_key =&gt; :facebook_user_uid
    has_many :groups, class_name: 'Group', :through =&gt; :group_invites, :source =&gt; :group
    ...
  end
  </code></pre>
  
  <p>Any help would be greatly appreciated!</p>