Best way to setup this relationship in activerecord

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.

On Aug 30, 6:40 am, Nathan E. <rails-mailing-l…@andreas-
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.

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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs