How can one use the same lookup table for multiple columns in the same table? e.g. let''s say I have a lookup table with automobile makes (Audi, BMW, Chevrolet) , and then a "drivers" table with columns for primary car make, secondary car make, etc. I can''t very well name them both make_id...? Many thanks, Brian --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom
2007-Mar-07 23:14 UTC
Re: One Lookup Table for Multiple Properties in a Model
> How can one use the same lookup table for multiple columns in the same > table? > > e.g. let''s say I have a lookup table with automobile makes (Audi, BMW, > Chevrolet) , and then a "drivers" table with columns for primary car > make, secondary car make, etc. I can''t very well name them both > make_id...?Change the key name in your has_one/belongs_to lines with: :foreign_key - specify the foreign key used for the association. By default this is guessed to be the name of this class in lower-case and "_id" suffixed. So a Person class that makes a has_one association will use "person_id" as the default foreign_key. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You''ll need :class_name, too. class Driver belongs_to :primary_make, :class_name => ''Make'', :foreign_key => ''pri_make_id'' belongs_to :secondary_make, :class_name => ''Make'', :foreign_key => ''sec_make_id'' belongs_to :favorite_make, :class_name => ''Make'', :foreign_key => ''the_best_make_id'' ... -- Wes On Mar 7, 5:14 pm, Philip Hallstrom <r...-SUcgGwS4C16SUMMaM/qcSw@public.gmane.org> wrote:> > How can one use the same lookup table for multiple columns in the same > > table? > > > e.g. let''s say I have a lookup table with automobile makes (Audi, BMW, > > Chevrolet) , and then a "drivers" table with columns for primary car > > make, secondary car make, etc. I can''t very well name them both > > make_id...? > > Change the key name in your has_one/belongs_to lines with: > > :foreign_key - specify the foreign key used for the association. By > default this is guessed to be the name of this class in lower-case and > "_id" suffixed. So a Person class that makes a has_one association will > use "person_id" as the default foreign_key.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---