Name_scope and has_many associations

Hey people,

I’m using named_scope in some associations and I’ve run into a bit of a
problem. For a simple association between two tables, named_scope works

class Person < AR:Base
has_many :cars

class Car < AR:Base
belong_to :person
named_scope :toyota, :conditions => {:make => ‘toyota’}
end works as expected.

Now let’s say you have a more complex association. Say, a country has
regions, a region has districts and a district has people.

class country < AR:Base
has_many :regions
has_many :people, :finder_sql =>
'SELECT p.* ’ +
'FROM people p, regions r, districts d ’ +
'WHERE r.country_id = #{id} AND d.region_id = AND ’ +
‘p.district_id =’
class region < AR:Base
belongs_to :country
has_many :districts
class district < AR:Base
belongs_to :region
has_many :people
class person < AR:Base
belongs_to :district
named_scope :high_earners, :conditions => [“salary_level > 6”]

antigua.people # all the people in all the districts and regions of

However antigua.people.high_earners returns nil. Why is this?

Thanks for any ideas you throw my way.

Just realized the problem. For complex has_many associations built
using a custom finder sql, the find_in_collection method is not added!

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