One Lookup Table for Multiple Properties in a Model


#1

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


#2

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.


#3

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