Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to design/model a has many relationship that has a meaningful join table?

I wasn't able to put well into words (in Question title) what I'm trying to do, so in honor of the saying that an image is worth a thousand words; In a nutshell what I'm trying to do is..

enter image description here

Basically, what I have is A Teacher has many Appointments and A Student has many Appointments which roughly translates to:

enter image description here

I'm trying to stay away from using the has_and_belongs_to_many macro, because my appointments model has some meaning(operations), for instance it has a Boolean field: confirmed.

So, I was thinking about using the has_many :through macro, and perhaps using an "Appointable" join table model? What do you guys think?

The Scenario I'm trying to code is simple;

  1. A Student requests an Appointment with a Teacher at certain Date/Time
  2. If Teacher is available (and wants to give lesson at that Date/Time), She confirms the Appointment.

I hope you can tell me how would you approach this problem? Is my assumption of using the has_many :through macro correct?

Thank you!

like image 401
jlstr Avatar asked Mar 13 '13 15:03

jlstr


Video Answer


1 Answers

Both teachers and students could inherit from a single class e.g. Person. Then create an association between Person and Appointments. This way you keep the architecture open so that if in the future you want to add 'Parents' then they could easily be integrated and may participate in appointments.

It may not be completely straightforward how you do the joins with the children classes (Students, Parents, Teachers). It may involve polymorphic relationships which I don't particularly like. You should though get away with a single join table.

In any case, you want to design so that your system can be extended. Some extra work early on will save you a lot of work later.

like image 118
Dimitris Avatar answered Oct 04 '22 22:10

Dimitris