DB Schema for Appointment Booking App: What is the correct relationship between Doctors, Appointments, TimeSlots, Patients?

送分小仙女□ 提交于 2019-12-03 22:46:09
abcm1989

I would add a boolean field to TimeSlot and write a custom validation allowing for conditional double-booking on TimeSlot. Associations below. (With the boolean field in your migration for TimeSlots, you could get rid of the FilledTimeSlots table.)

Office < ActiveRecord::Base
  has_many :doctors

Doctor < ActiveRecord::Base
  has_many :patients, through: :appointments
  has_many :appointments
  belongs_to :office

Patient < ActiveRecord::Base
  has_many :doctors, through: :appointments 
  has_many :appointments

Appointment < ActiveRecord::Base
  belongs_to :patient
  belongs_to :doctor
  belongs_to :chair
  belongs_to :timeslot

TimeSlot < ActiveRecord::Base
  validates_with :availability, unless: "appointments.nil?"
  validates_with :workday
  has_many :appointments
  has_many :chairs, through: :appointments 
#you could then specify a maximum of 2 chairs in your validation depending on the appointment type, something like this:

def availability
  if self.chairs.count == 0
     self.booked? == false
  elsif self.chairs.count == 2
     self.booked? == true
  elsif self.chairs.count == 1 and self.appointment.type == "cleaning"
     self.booked? == false
  else
      self.booked? == true
  end
end


Chair < ActiveRecord::Base
  has_many :appointments
  belongs_to :timeslot, through: :appointment
end

------------ Alternate Answer by Max Williams ------------------

 Doctor
   has_many :appointments

 Patient
   has_many :appointments

 Office 
   has_many :chairs  

 Chair
   #key-fields: office_id
   belongs_to :office
   has_many :appointments  

 TimeSlot
  #key-fields: starts_at, :ends_at  

Appointment
  #key-fields: doctor_id, chair_id, time_slot_id, patient_id
  belongs_to :doctor
  belongs_to :chair
  belongs_to :time_slot  
  belongs_to :patient

I would do it like this:

Doctor
  has_many :appointments

Patient
  has_many :appointments

Office 
  has_many :chairs  

Chair
  #key-fields: office_id
  belongs_to :office
  has_many :appointments  

TimeSlot
  #key-fields: starts_at, :ends_at  

Appointment
  #key-fields: doctor_id, chair_id, time_slot_id, patient_id
  belongs_to :doctor
  belongs_to :chair
  belongs_to :time_slot  
  belongs_to :patient

The booking form is going to consist of getting all available time slots, and then, for each time slot, showing chairs which don't have an appointment in that time slot.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!