best way to model this

Let's say I have a car table that has many passengers so obviously the
passenger table will have a foreign key to car. The Car model has_many
Passengers. A car can have one and only one driver. What's the best
way to model this relationship?

Hand coded get and set driver methods in the car class with one of the
following keys.
1) a flag in the passenger table noting the passenger as driver
2) a passenger foreign key in car to note driver

Extend passenger to make a Driver model and do either:

3) Car belongs_to Driver

4) Car has_one Driver

Or something else?


I would do:

Passengers belong_to Car
Car has_many Passengers
Car has_one Driver, :class => Passenger

Dieter Lunn

I'd do something similar.

Actually if I had the freedom, I'd probably rename the class Passenger
to something like Person since Driver and Passenger are really roles,
not entities.

class Person < ActiveRecord::Base
   belongs_to :car

class Car < ActiveRecord::Base
   belongs_to :driver, :class => ;person
   has_many :passengers, :class => person