Forum: Ruby on Rails One Lookup Table for Multiple Properties in a Model

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bdf31c9ed36b1e66e2c7d819500aa47f?d=identicon&s=25 Brian (Guest)
on 2007-03-07 23:58
(Received via mailing list)
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
2f9a03aa0fcfe945229cb6126eda2cb2?d=identicon&s=25 Philip Hallstrom (Guest)
on 2007-03-08 00:08
(Received via mailing list)
> 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.
A332dcb1e051f733dc9a9bd78df3b560?d=identicon&s=25 wesgarrison (Guest)
on 2007-03-08 00:52
(Received via mailing list)
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
This topic is locked and can not be replied to.