How to simulate "meetings" in Rails?

Here is the scenario I am facing:

An appointment may be planned for:

  • Today
  • some time during the week
  • on a specific date

Thus, attributes can be different for each destination “type”.

I thought about these models and used it with STI, but I'm not sure I'm on the right track:

class Appointment < ActiveRecord::Base
class TodayAppointment < Appointment
class WeekAppointment < Appointment
class SpecificDateAppointment < Appointment

Table:

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"

What is the best way to simulate this?

Is this a good candidate for one-dimensional inheritance?

UPDATE

Thanks @Mike, @SpyrosP for helping so far. I came up with the options that I have below.

These are the “views” of the database tables and how they look.

Which one is most suitable?

------------------------------------------------------------------------
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          |
------------------------------------------------------------------------
+3
source share
2

, , . , .

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

Source: https://habr.com/ru/post/1782364/


All Articles