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

时间:2010-12-27 22:06:23

标签: model ruby-on-rails-3 sti

这是我面临的情景:

可以安排约会:

  1. 今天
  2. 一周中的某个时间
  3. 在特定日期
  4. 因此,每个“约会类型”的属性可能不同。

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

2 个答案:

答案 0 :(得分:1)

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

这是vanilla 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

请注意每种具体类型透明地选择适当的映射器的能力。考虑将其与Dependency Injection结合使用,以便于测试。

答案 1 :(得分:0)

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

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

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