bpokosh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-May-25 16:05 UTC
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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 25 May 2008, at 17:05, bpokosh-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > 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? >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> >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---