Has_many associations

I have two tables, Zip (representing a zip code) and District
(representing different districts a state or county is divided into).
Zip references District multiple times, for example:

belongs_to :x_district, :class_name => “District”
belongs_to :y_district, :class_name => “District”
belongs_to :z_district, :class_name => “District”

My question is how the corresponding has_many relationships should be
set up in the District model. Each district will only be referenced by
a single field in the Zip table. District 1 might be referenced
through x_district for many different zip codes, but it would never be
referenced by y_district or z_district. I could have multiple
has_manys:

has_many :x_zips, :class_name => “zip”, :foreign_key =>
“x_district_id”
has_many :y_zips, :class_name => “zip”, :foreign_key =>
“y_district_id”
has_many :z_zips, :class_name => “zip”, :foreign_key =>
“z_district_id”

But I’d like just a single has_many :zips that lumps them all
together. Any thoughts on the best way to handle this relationship?

On 25 May 2008, at 17:05, [email protected] wrote:

set up in the District model. Each district will only be referenced by
“z_district_id”

But I’d like just a single has_many :zips that lumps them all
together. Any thoughts on the best way to handle this relationship?

You could probably use a custom finder_sql to associate those zips
with x_district = foo.id or y_district=foo.id etc… (your sql will be
interpolated in the context of the model). Won’t be very pretty though.

Fred