title: Rails named_scope across multiple tables
body: <p>I'm trying to tidy up my code by using named_scopes in Rails 2.3.x but where I'm struggling with the has_many :through associations. I'm wondering if I'm putting the scopes in the wrong place... </p>
  
  <p>Here's some pseudo code below. The problem is that the :accepted named scope is replicated twice... I could of course call :accepted something different but these are the statuses on the table and it seems wrong to call them something different. Can anyone shed light on whether I'm doing the following correctly or not? </p>
  
  <p>I know Rails 3 is out but it's still in beta and it's a big project I'm doing so I can't use it in production yet.</p>
  
  <pre><code>class Person &lt; ActiveRecord::Base
    has_many :connections
    has_many :contacts, :through =&gt; :connections
  
    named_scope :accepted, :conditions =&gt; ["connections.status = ?", Connection::ACCEPTED]
    # the :accepted named_scope is duplicated
    named_scope :accepted, :conditions =&gt; ["memberships.status = ?", Membership::ACCEPTED]
  end
  
  class Group &lt; ActiveRecord::Base
    has_many :memberships
    has_many :members, :through =&gt; :memberships
  end
  
  class Connection &lt; ActiveRecord::Base
    belongs_to :person
    belongs_to :contact, :class_name =&gt; "Person", :foreign_key =&gt; "contact_id"
  end
  
  class Membership &lt; ActiveRecord::Base
    belongs_to :person
    belongs_to :group
  end
  </code></pre>
  
  <p>I'm trying to run something like person.contacts.accepted and group.members.accepted which are two different things. Shouldn't the named_scopes be in the Membership and Connection classes? </p>
  
  <p>However if you try putting the named scopes in the Membership and Connection classes then you get this error (because Person.find(2).contacts returns an array of Persons which doesn't have an 'accepted' method:</p>
  
  <pre><code>&gt;&gt; Person.find(2).contacts.accepted
  NoMethodError: undefined method `accepted' for #&lt;Class:0x108641f28&gt;
  </code></pre>
  
  <p>One solution is to just call the two different named scope something different in the Person class or even to create separate associations (ie. has_many :accepted_members and has_many :accepted_contacts) but it seems hackish and in reality I have many more than just accepted (ie. banned members, ignored connections, pending, requested etc etc)</p>