[更新的模式:http://i.imgur.com/fX9CGNh.png]
我的任务是开发一个专为小型医疗办公室设计的预约系统。这是一个Rails 3.2应用程序。
我在设计一个对我有意义的database schema时遇到了困难。
问题: 根据以下信息,医生,患者,约会,椅子和时间点之间的正确关系是什么?
患者需要到医生办公室预约。根据约会的类型,每个约会被安排为一个或多个相邻的time_slots,并且是否可以为具有相同start_time和end_time的time_slots安排两个约会由约会类型确定。 (根据约会类型允许双重预订。)
App规格:
我的关系:
Office < ActiveRecord::Base
has_many :doctors
Doctor < ActiveRecord::Base
has_many :patients
belongs_to :offices
Patient < ActiveRecord::Base
belongs_to :doctor
has_many :appointments
Appointment < ActiveRecord::Base
belongs_to :patient
has_many :filled_time_slots
FilledTimeSlot < ActiveRecord::Base
belongs_to :appointment
belongs_to :time_slot
TimeSlot < ActiveRecord::Base
has_many :filled_time_slots
belongs_to :chair
Chair < ActiveRecord::Base
has_many :time_slots
答案 0 :(得分:3)
我会这样做:
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
预订表格将包括获取所有可用的时段,然后,对于每个时段,显示在该时段中没有预约的椅子。
答案 1 :(得分:3)
我会在TimeSlot中添加一个布尔字段,并编写一个自定义验证,允许在TimeSlot上进行条件双重预订。下面的协会。 (使用TimeSlots迁移中的布尔字段,可以摆脱FilledTimeSlots表。)
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
------------ 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