这是多态性的有效用法吗?如果我应该如何声明这种关系?

时间:2011-10-23 17:44:00

标签: ruby-on-rails ruby polymorphism

我有一个约会模型,可以由导师或学生初始化。一旦初始化,另一方可以接受或拒绝。

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

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

谢谢。

2 个答案:

答案 0 :(得分:5)

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

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

多态参与者的一个例子

<强>预约

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模块包含导师和学生的方法,因此您可以执行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

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

以下是有关创建模型的有用资源:http://henrik.nyh.se/2008/02/rails-model-extensions

答案 1 :(得分:0)

我意识到在我的情况下我不需要多态性。相反,我需要条件主动关系:

Appointment.rb

 has_one :tutor, :class_name => "Participant", :foreign_key => :appointment_id, :conditions => {:invitable_type => "Tutor"},  :dependent => :destroy