# Best way to setup this relationship in activerecord

**URL:** <https://www.ruby-forum.com/t/best-way-to-setup-this-relationship-in-activerecord/113346>\
**Category:** Rails\
**Created:** [August 30, 2007, 7:40am UTC](https://www.ruby-forum.com/t/best-way-to-setup-this-relationship-in-activerecord/113346 "2007-08-30T07:40:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xgamerx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/d2c977/32.png) [@xgamerx](https://www.ruby-forum.com/u/xgamerx)\
**Post date:** [August 30, 2007, 7:40am UTC](https://www.ruby-forum.com/t/best-way-to-setup-this-relationship-in-activerecord/113346/1 "2007-08-30T07:40:41Z")

</div>

Hello guys, I am starting a new project and I am relatively  
inexperienced with Database design. I would absolutely love some  
guidance on the best way to setup a certain relationship in  
ActiveRecord.

I am created a searchable directory of doctors where doctors can  
register for an account and be added to a searchable database. Patients  
can then search this database to find a doctor in their area. The idea  
is pretty simple but discovered that doctors and patients will largely  
have the same information stored in the database. For instance, here is  
the doctor and patient models and their fields:

Doctor:

- id
- first\_name
- last\_name
- title
- gender
- birthday
- created\_at
- practice
- specialty

Patient:

- id
- first\_name
- last\_name
- title
- gender
- birthday
- created\_at

Obviously there is a ton of overlap in those fields and common  
convention would tell me to use inheritance.

Something like this:

User:

- id
- first\_name
- last\_name
- title
- gender
- birthday
- created\_at

Doctor:

- practice
- specialty

Patient:  
(no extra fields)

Should I listen to my gut there and use single-table inheritance via the  
Type column in active record? Is this the “right” way to do this. I am  
new to rails and databases and I want to learn “best practices” so to  
speak. Please help me understand the options, and what you believe is  
the best choice. Should I keep them two separate tables, or use  
inheritance or some other option that I don’t even know about.

---

<div class="post-metadata">

**Author:** ![xgamerx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/d2c977/32.png) [@xgamerx](https://www.ruby-forum.com/u/xgamerx)\
**Post date:** [August 30, 2007, 1:44pm UTC](https://www.ruby-forum.com/t/best-way-to-setup-this-relationship-in-activerecord/113346/2 "2007-08-30T13:44:14Z")

</div>

On Aug 30, 6:40 am, Nathan E. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> the doctor and patient models and their fields:
> 
> - specialty  
> Obviously there is a ton of overlap in those fields and common
> - birthday  
> Type column in active record? Is this the “right” way to do this. I am  
> new to rails and databases and I want to learn “best practices” so to  
> speak. Please help me understand the options, and what you believe is  
> the best choice. Should I keep them two separate tables, or use  
> inheritance or some other option that I don’t even know about.  
> –  
> Posted viahttp://www.ruby-forum.com/.

Hi Nathan,

I’m only just starting out with Rails too, but here are my thoughts…

Using inheritance in ordinary object-oriented languages gives you a  
few benefits (and a few drawbacks!). Primarily, these are reuse and  
polymorphism.

If you are going to want to define a number of similar methods on  
doctor and patient (including performing much of the same validation),  
it may be worthwhile using inheritance. This way, you can write less  
unit tests, and reduce duplication of code.

Furthermore, if you are likely to want to access all of the users in  
the system, you can just iterate over Users rather than Doctors and  
Patients.

If you are interested in using inheritance, there are a number of ways  
in which you can represent this relationship in your database. I would  
recommend reading the relevant section in Agile Web D. with  
Rails, if you have a copy of this book. And if you don’t, I can’t  
recommend it highly enough; it’s fantastic.

Hope this helps!

Cheers,  
Louis.

---

<div class="post-metadata">

**Author:** ![xgamerx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/d2c977/32.png) [@xgamerx](https://www.ruby-forum.com/u/xgamerx)\
**Post date:** [August 30, 2007, 11:47pm UTC](https://www.ruby-forum.com/t/best-way-to-setup-this-relationship-in-activerecord/113346/3 "2007-08-30T23:47:30Z")

</div>

Thanks for your help Louis. I have found an alternative way to do this  
which I think is actually better. The basics is that all the overlapping  
fields go into a seperate model/table and then polymorphic associations  
are used to include the fields into each model. Check out the following  
link for more information:

[http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssociations](http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssociations)

Then you can simply use it like this:

@doctor = Doctor.new(:specialty =\> ‘family’, …)  
@doctor.create\_detail(:first\_name =\> ‘joe’, …)  
@doctor.save

@patient = Patient.new()  
@patient.create\_detail(:first\_name =\> ‘beth’, …)  
@patient.save

@doctor.detail.first\_name =\> ‘joe’  
@patient.detail.first\_name =\> ‘beth’

Thanks again, and if anyone thinks this way is not as good as STI please  
let me know.
