3

这是我面临的情况:

可以安排约会:

  1. 今天
  2. 一周中的某个时间
  3. 在特定日期

因此,每个约会“类型”的属性可能不同。

我正在考虑这些模型并将其与 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          |
------------------------------------------------------------------------
4

2 回答 2

1

你很接近,但看起来你可以从使用Class Table Inheritance中受益。我的理由是,每种具体类型都有不同的属性。

这是香草 Ruby 中的一些示例代码。我相信这比我能给出的任何描述都更容易理解。

class Appointment
    def persist
        raise "Must be implemented."
    end
end

class TodayAppointment < Appointment
    def persist
        TodayAppointmentMapper save self
    end
end

class WeekAppointment < Appointment
    def persist
        WeekAppointmentMapper save self
    end
end

class Mapper
    def save aAppointment
        raise "Must be implemented."
    end
end

class TodayAppointmentMapper < Mapper
    def save aAppointment
        # Specfic Today Appointment persistence details.
    end
end

class WeekAppointmentMapper < Mapper
    def save aAppointment
        # Specfic Week Appointment persistence details.
    end
end

请注意每种具体类型能够透明地选择适当的映射器。考虑将其与依赖注入结合起来,以便于测试。

于 2010-12-27T22:12:38.520 回答
0

我还没有使用它们是真诚的,但看起来你需要多态模型。

http://railscasts.com/episodes/154-polymorphic-association

请看一看。这是一种为不同场合创建多态模型的方法,例如您的示例。

于 2010-12-27T22:08:33.313 回答