Referencing associated records in a named scope

Hi all

I have a situation like this:

class Country
has_many :policy_indicator_ratings
has_many :policy_indicators, :through => :policy_indicator_ratings

class PolicyIndicatorRating
belongs_to :country
belongs_to :policy_indicator

class PolicyIndicator
has_many :policy_indicator_ratings
named_scope :water, :conditions => [“sector = ?”, ‘water’]
named_scope :sanitation, :conditions => [“sector = ?”,‘sanitation’]

Now, i want to say something like

@country.policy_indicators.water.each do |indicator|
#do something with this country’s rating for this indicator

It feels like i’m now forced to do something like this inside the loop:

rating =

This seems clumsy - i’ve gone through the join model to get the
indicator, and now i have to manually pull the join record out again.

Is there a way to set up the named scope on the join model, something
like this?

#this won’t work but gets the idea across
named_scope :water, :conditions => (self.policy_indicator.sector ==


Or, is there a nicer way to organise my schema to seperate out water
indicators from sanitation indicators?

Grateful for any advice.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs