gpt4 book ai didi

ruby-on-rails - 这是对多态性的有效使用吗?如果是,我应该如何声明这种关系?

转载 作者:数据小太阳 更新时间:2023-10-29 08:33:57 25 4
gpt4 key购买 nike

我有一个 Appointment 模型,可以由 Tutor 或 Student 初始化。一旦被一方初始化,另一方可以接受或拒绝。

我将我的模型设计为:约会和参与者。 Participant 有两个属性:participant_id 和 participant_type ("Tutor"/"Student")。我想声明 Appointment has_one Tutor, has_many Students 使用多态。

我的问题是:这是对多态性的有效使用吗?如果是,那么我应该如何声明这种关系和外键?如果不是,那是为什么?

谢谢。

最佳答案

当您拥有在不同实体(例如学生和导师)之间共享的共同属性(例如参与约会的能力)时,请使用多态性。我认为你的情况需要参与者的多态性,而不是约会。

问问自己:是否有不同类型的约会或不同类型的参与者?从您提供的信息来看,您似乎有一种约会,以及不同种类的参与者。

多态参与者的例子

预约

class Appointment < ActiveRecord::Base
has_many :participants
has_one :tutor, :through => participants
has_many :students, :through => participants
end

学生

class Student < ActiveRecord::Base
has_many :appointments, :as => appointable
end

导师

class Tutor < ActiveRecord::Base
has_many :appointments, :as => :appointable
end

参与者

# This model joins your appointable entities (Tutors and Students)
# to Appointments
class Participant < ActiveRecord::Base
belongs_to :appointment
belongs_to :appointable, :polymorphic => true
end

就声明外键而言,Rails 会为您处理。

参与者迁移

class CreateParticipants < ActiveRecord::Migration
def up
create_table :partipants do |t|
t.references :appointment
t.references :appointable, :polymorphic => true
end
end

def down
drop_table :participants
end
end

为了更好地理解 Rails 如何将诸如 polymorphic 之类的关键字转换为 SQL 关联,请参阅指南:http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

我认为状态机是一个有趣的选择——我没有任何 Ruby/Rails 状态机项目的经验,所以我不能就此给你建议。

调度

这是关于如何设置日程安排的不完整描述。希望这足以让您入门。

将这些方法添加到约会:

class Appointment < ActiveRecord::Base

# Anybody may request a new appointment,
# specifying the initiator, and other participants
# they would like to attend.
def self.request requester, requested_participants=nil
a = self.new
a.status = "requested"
a.requester = requester
a.request_participation_of requested_participants
a.save!
end

# Anybody may call this method
# to request that the appointment be
# rescheduled, specifying the requester
def reschedule requester
self.status = "reschedule_requested"
requester.participation_in(self).update_attribute :status=> "requester_of_reschedule"
self.participants.where("appointable_id != ?", requester.id)
.update_all :status => "reschedule_requested"
self.save!
end

protected

def requester= requester
requester.participation_in(self).update_attribute :status => "requester"
end

def request_participation_of participants
if participants.is_a? Array
participants.each do |participant|
participant.request_participation_in self
end
else
request_participation_of [participants]
end
end
end

Scheduling 模块包含 Tutors 和 Students 的方法,因此您可以执行 student_3.request_appointment tutor_1tutor_1.reschedule_appointment appointment_4 等操作。

lib/appointments/scheduling.rb:

module Appointments::Scheduling
# When a Student or Tutor's participation
# in an Appointment has been requested,
# he/she indicates acceptance of the request
# with this method
def accept_participation_in appointment
self.participation_in(appointment).update_attribute :status => "accepted"
end

# Same as above, to decline the request
def decline_participation_in appointment
self.participation_in(appointment).update_attribute :status => "declined"
end

# This method finds the Student or Tutor's
# Participant object for a particular Appointment
def participation_in appointment
Participant.where(:appointment_id => appointment.id)
.find_by_appointable_id self.id
end

# A student or tutor can request a new
# Appointment with a participant or
# group of participants with this method
def request_appointment participants
Appointment.request self, participants
end

# This Student or Tutor's participation
# in an appointment can be requested with
# this method
def request_participation_in appointment
Participant.find_or_create_by_appointment_id_and_appointable_id(
:appointment_id => appointment.id,
:appointable_id => self.id
)
end

# This Student or Tutor's confirmation of
# a scheduled Appointment may be requested
# with this method
def request_reschedule_of appointment
new_status = "reschedule_requested"
if participant.id.eql? requester.id
new_status = "requester_of_reschedule"
end
self.participation_in(appointment).update_attribute :status => new_status
end

# A Student or Tutor may reschedule a
# particular Appointment with this method
def reschedule_appointment appointment
appointment.reschedule self
end
end

一旦这些模块就位,您就可以将它们包含在适当的实体中:

class Appointment < ActiveRecord::Base
include Appointments::Schedulable
end

class Student < ActiveRecord::Base
include Appointments::Scheduling
end

class Tutor < ActiveRecord::Base
include Appointments::Scheduling
end

我的示例还要求您向 Appointment 和 Participant 添加一个 status 字段。我最终会创建一个 AppointmentStatus 和一个 ParticipantStatus - 但是,首先我会让系统在没有它的情况下工作。

这里有一个有用的资源,可以帮助您创建用于模型的模块:http://henrik.nyh.se/2008/02/rails-model-extensions

关于ruby-on-rails - 这是对多态性的有效使用吗?如果是,我应该如何声明这种关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7867931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com