这是多态性的有效使用吗?如果是,我应该如何声明这种关系?
我有一个预约模型,可以由导师或学生初始化。一旦一侧初始化,另一侧就可以接受或拒绝。
我将模型设计为:约会和参与者。参与者有两个属性:participant_id 和participant_type(“导师”/“学生”)。我想使用多态声明 Appointment has_one Tutor, has_many Students。
我的问题是:这是多态性的有效使用吗?如果是,那么我应该如何声明这种关系和外键?如果不是,那为什么?
谢谢。
I have an Appointment model, which can be initialized by Tutor or Student. Once initialized by one side, the other side can accept or decline.
I design my models as: Appointment, and Participant. Participant has two attributes: participant_id and participant_type ("Tutor"/"Student"). I would like to declare Appointment has_one Tutor, has_many Students using polymorphic.
My questions are: Is this a valid use of polymorphism? If yes, then how should I declare this relationship and the foreign keys? If no, then why?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您具有在不同实体(例如学生和导师)之间共享的公共属性(例如参与约会的能力)时,请使用多态性。我认为您的情况需要参与者的多态性,而不是约会。
问问自己:是否有不同类型的约会或不同类型的参与者?从您提供的信息来看,您似乎有一种约会和不同类型的参与者。
多态参与者的示例
约会
学生
导师
参与者
至于声明你的外键,Rails 会负责给你的。
参与者迁移
要更好地了解 Rails 如何将
polymorphic
等关键字转换为 SQL 关联,请参阅指南:http://guides.rubyonrails.org/association_basics.html#polymorphic-associations我认为状态机是一个有趣的选择 - 我没有任何 Ruby/Rails 状态机项目的经验,所以我不能给你这方面的建议。
调度
这是如何设置调度的不完整图景。希望这足以让您开始。
将这些方法添加到约会:
安排模块包含针对导师和学生的方法,因此您可以执行诸如
student_3.request_appointment coach_1
或tutor_1.reschedule_appointment Appointment_4
之类的操作。lib/appointments/scheduling.rb
:这些模块就位后,您可以将它们包含在适当的实体中:
我的示例还要求您向两个模块添加一个
status
字段任命和参与者。我最终会创建一个 AppointmentStatus 和一个 ParticipantStatus - 首先我会让系统在没有它们的情况下工作。以下是有关创建模型中使用的模块的有用资源:http:// henrik.nyh.se/2008/02/rails-model-extensions
Use polymorphism when you have a common property (such as the ability to participate in appointments) shared among different entities (such as Students and Tutors). I think your situation calls for polymorphism for Participants, rather than Appointments.
Ask yourself: are there different types of Appointments, or different types of Participants? From the information you've provided, it seems like you have one kind of Appointment, and different kinds of Participants.
An example of polymorphic Participant
Appointment
Student
Tutor
Participant
As far as declaring your foreign keys goes, Rails takes care of that for you.
Migration for Participant
For a better understanding of how Rails translates keywords such as
polymorphic
into SQL associations, see the guide: http://guides.rubyonrails.org/association_basics.html#polymorphic-associationsI think state machines are an interesting option - I don't have experience with any of the Ruby/Rails state machine projects, so I can't give you advice on that.
Scheduling
This is an incomplete picture of how to set up scheduling. Hopefully, it should be enough to get you started.
Add these methods to Appointment:
The Scheduling module contains methods for Tutors and Students, so you can do things like
student_3.request_appointment tutor_1
ortutor_1.reschedule_appointment appointment_4
.lib/appointments/scheduling.rb
:Once these modules are in place, you can include them in the appropriate entities:
My example also requires that you add a
status
field to both Appointment and Participant. I would eventually create an AppointmentStatus and a ParticipantStatus - first I would get the system working without that, however.Here is a helpful resource on creating modules for use in your models: http://henrik.nyh.se/2008/02/rails-model-extensions
我意识到就我而言,我不需要多态性。相反,我需要条件主动关系:
Appointment.rb
I realized that in my case I don't need polymorphism. Instead, I need conditional active relation:
Appointment.rb