这是我面临的情况:
可以安排约会:
- 今天
- 一周中的某个时间
- 在特定日期
因此,每个约会“类型”的属性可能不同。
我正在考虑这些模型并将其与 STI 一起使用,但我不确定我是否走在正确的轨道上:
class Appointment < ActiveRecord::Base
class TodayAppointment < Appointment
class WeekAppointment < Appointment
class SpecificDateAppointment < Appointment
桌子:
string, Type #type of the appointment (TodayAppointment, WeekAppointment...)
datetime, When #data used when type field is "SpecificDateAppointment"
string, Something #used when type field is "TodayAppointment"
对此进行建模的最佳方法是什么?
这是单表继承的好选择吗?
更新
感谢@Mike,@SpyrosP 到目前为止的帮助。我想出了下面的选项。
这些是数据库表的“视图”以及它们的外观。
哪一个看起来最合适?
------------------------------------------------------------------------
Option A--(Polymorphic Association)
------------------------------------------------------------------------
|patients | day_appointments | week_appointments
| appointment_type | data | data
| appointment_id | |
------------------------------------------------------------------------
Option B--(Child references parent) (What is this pattern called?)
------------------------------------------------------------------------
|patients | day_appointments | week_appointments
| | patient_id | patient_id
------------------------------------------------------------------------
Option C--(Polymorphic Association + Single Table Inheritance of appointments)
------------------------------------------------------------------------
|patients | appointments |
| appointment_type | type |
| appointment_id | day_data |
| | week_data |
------------------------------------------------------------------------
Option D--(Child references parent + Single Table Inheritance of appointments)
------------------------------------------------------------------------
|patients | appointments |
| | type |
| | day_data |
| | patient_id |
------------------------------------------------------------------------