Named scopes for instances I have following relationship schema:

class Participation
  belongs_to :toon
  belongs_to :person

  validates :role, :presence =&gt; true
end

class Toon
  has_many :participations
  has_many :people, :through =&gt; :participations
end

class Person
  has_many :participations
  has_many :toons, :through =&gt; :participations
end


I want to get people association related to toon, but filtered by role attribute value. I need something like toon.people_with_role('producer').

class Toon &lt; ActiveRecord::Base
  has_many :participations
  has_many :people, :through =&gt; :participations

  def people_with_role(role)
    Participation.where(:toon_id =&gt; self, :role =&gt; role).map(&amp;:person)   
  end
end


It works, but I'm not sure that it is a best way to do it.
Here is another way to filter people by role:

class Participation &lt; ActiveRecord::Base
  belongs_to :toon
  belongs_to :person

  validates :role, :presence =&gt; true
end

class Person &lt; ActiveRecord::Base
  has_many :participations
  has_many :toons, :through =&gt; :participations
end

class Toon &lt; ActiveRecord::Base
  has_many :participations
  has_many :people, :through =&gt; :participations do
    def with_role(role)
      where('participations.role' =&gt; role)
    end
  end
end


Now, you can get the people fitered by role: toon.people.with_role('producer').