Model 如何在Rails中为“约会”建模?

Model 如何在Rails中为“约会”建模?,model,ruby-on-rails-3,sti,Model,Ruby On Rails 3,Sti,以下是我面临的情况: 预约时间可安排为: 今天 一周中的某个时间 在特定日期 因此,每种约会类型的属性都可能不同 我想到了这些模型,并将其用于STI,但我不确定我是否走上了正确的道路: class Appointment < ActiveRecord::Base class TodayAppointment < Appointment class WeekAppointment < Appointment class SpecificDateAppointment < A

以下是我面临的情况:

预约时间可安排为:

今天 一周中的某个时间 在特定日期 因此,每种约会类型的属性都可能不同

我想到了这些模型,并将其用于STI,但我不确定我是否走上了正确的道路:

class Appointment < ActiveRecord::Base
class TodayAppointment < Appointment
class WeekAppointment < Appointment
class SpecificDateAppointment < Appointment
最好的建模方法是什么

这是单表继承的一个很好的候选者吗

更新

感谢@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          |
------------------------------------------------------------------------

我还没有用过它们,但看起来你需要多态模型


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

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

下面是一些香草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

请注意,每个具体类型都可以透明地选择适当的映射器。考虑将此与更容易的测试相结合。

我认为当一个模型需要属于许多其他模型时,使用多态性关联,例如,许多模型可能需要一个地址,因此地址可以属于许多模型。多态关联在这里不适用吗?或者是吗?没错,但是每个模型都有自己的特点,就像你的模型一样。然后,您可以添加has\u many:todaysappoints、:as=>:appointment之类的内容。我想那会有用的。这看起来很像btw@SpyrosP:但是,我真正需要的是今天有一个约会,或者一周有一个约会,或者有一个特定的约会。。我该如何执行?你的意思是你需要写一个星期的约会,或者可以做一些像星期约会时间、今天约会时间等的事情?后一种情况会发生,但是如果您明确地想要指定has_one today_约会,那么有理由这样做吗?我还没有听说过类表继承!是什么?更新了我的答案。我省略了它,因为您提到了单表继承。它们都来自Martin Fowler的书《企业应用程序架构模式》。附言:读一读这本该死的书,它会给你带来设计上的乐趣。谢谢你的建议,我很快会的。所以你的建议是我使用单独的表格,每种约会类型一个?那么,我如何确保拥有预约的某些患者模型只有三种预约类型中的一种?我应该让病人属于什么?
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