# Best way to model this

**URL:** <https://www.ruby-forum.com/t/best-way-to-model-this/188339>\
**Category:** Rails\
**Created:** [May 10, 2010, 2:46pm UTC](https://www.ruby-forum.com/t/best-way-to-model-this/188339 "2010-05-10T14:46:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [May 10, 2010, 2:46pm UTC](https://www.ruby-forum.com/t/best-way-to-model-this/188339/1 "2010-05-10T14:46:49Z")

</div>

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:

1. Car belongs\_to Driver

2. Car has\_one Driver

Or something else?

Thanks!

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [May 10, 2010, 2:53pm UTC](https://www.ruby-forum.com/t/best-way-to-model-this/188339/2 "2010-05-10T14:53:43Z")

</div>

I would do:

Passengers belong\_to Car  
Car has\_many Passengers  
Car has\_one Driver, :class =\> Passenger

Dieter L.  
[http://www.coder2000.ca](http://www.coder2000.ca)

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [May 10, 2010, 3:23pm UTC](https://www.ruby-forum.com/t/best-way-to-model-this/188339/3 "2010-05-10T15:23:35Z")

</div>

On Mon, May 10, 2010 at 8:52 AM, Dieter L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I would do:
> 
> Passengers belong\_to Car  
> Car has\_many Passengers  
> Car has\_one Driver, :class =\> Passenger

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  
end

class Car \< ActiveRecord::Base  
belongs\_to :driver, :class =\> ;person  
has\_many :passengers, :class =\> person  
end

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Github: [rubyredrick (Rick DeNatale) · GitHub](http://github.com/rubyredrick)  
Twitter: @RickDeNatale  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)
