本文介绍了Rails:使用find方法访问连接表以获得多态关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有一个通用的 TimeSlot 模型,它处理一个 start_at 和一个 end_at 时间跨度。几个模型派生自这个,但我指的是这个问题: AppointmentBlock 它是约会的集合 。我想验证一个 AppointmentBlock ,这样没有其他的 AppointmentBlocks 被安排到一个特定的 Employee 在同一时间范围内。由于 AppointmentBlock TimeSlot 具有多态关联,所以您必须访问 AppointmentBlock TimeSlot c $ c>的 start_at end_at >像这样: appt_block.time_slot.start_at 这意味着我需要在我的:conditions 为我的 find()方法调用。以下是我的代码:

  #inside my time_slot.rb model 
belongs_to:time_slot_role,:polymorphic => ; true

 

  #inside my appointment_block.rb模型
has_one:time_slot,:as => :time_slot_role,:dependent => :销毁
验证:employee_not_double_booked

def employee_not_double_booked
除非self.employee_id
#这个查找条件是不正确的,因为我需要加入time_slots来获得访问
#到start_at和end_at。我该怎么做?
blocks = AppointmentBlock.find(:first,
:conditions => ['employee_id =?and(start_at between?and?or end_at between?and?)',
self.employee_id ,self.time_slot.start_at,self.time_slot.end_at,
self.time_slot.start_at,self.time_slot.end_at])
#伪代码:
#收集一个预约的列表,在这个
#apointment块之后结束,在这个约会之前开始或者开始
#块结束与这个约会相关的
#块被分配的雇员
#如果count很大,那么0该员工已被双
#预订。

#如果发现一个块意味着这个员工正在获取
#double记录,所以引发一个错误
errors.addAppointmentBlock,
已经在此期间计划if blocks
end
end

由于 AppointmentBlock 没有 start_at end_at 我怎样才能加入使用 time_slots 表来获取这些条件?

解决方案

可以在find上使用:joins参数,类似如下:

$ p $ block $ AppointmentBlock.find(:first,
:conditions => ['employee_id =?and(time_slots.start_at between?and?or time_slots.end_at between?and?)',
self.employee_id,self.time_slot.start_at,self.time_slot.end_at,
self.time_slot.start_at,self.time_slot.end_at],
:joins =>'在time_slots.time_slot_上加入time_slots role_id = appointment_blocks.id')


Ok, I have a generic TimeSlot model that deals with a start_at and an end_at for time spans. A couple models derive from this but I'm referring to one in this question: AppointmentBlock which is a collection of Appointments. I want to validate an AppointmentBlock such that no other AppointmentBlocks have been scheduled for a particular Employee in the same time frame. Since AppointmentBlock has a polymorphic association with TimeSlot, you have to access the AppointmentBlock's start_at and end_at through the TimeSlot like so: appt_block.time_slot.start_at This means that I need to have some kind of join in my :conditions for my find() method call. Here is my code so far:

#inside my time_slot.rb model
belongs_to :time_slot_role, :polymorphic => true
#inside my appointment_block.rb model
has_one :time_slot, :as => :time_slot_role, :dependent => :destroy
validate :employee_not_double_booked

def employee_not_double_booked
  unless self.employee_id
    # this find's condition is incorrect because I need to join time_slots to get access
    # to start_at and end_at. How can I do this?
    blocks = AppointmentBlock.find(:first,
      :conditions => ['employee_id = ? and (start_at between ? and ? or end_at between ? and ?)',
      self.employee_id, self.time_slot.start_at, self.time_slot.end_at,
      self.time_slot.start_at, self.time_slot.end_at])
    # pseudo code:
    # collect a list of appointment blocks that end after this
    # apointment block starts or start before this appointment
    # block ends that are also associated with this appointment
    # blocks assigned employee
    # if the count is great then 0 the employee has been double
    # booked.

    # if a block was found that means this employee is getting
    # double booked so raise an error
    errors.add "AppointmentBlock",
      "has already been scheduled during this time" if blocks
  end
end

Since AppointmentBlock doesn't have a start_at or an end_at how can I join with the time_slots table to get those conditions to work?

解决方案

You can use the :joins parameter on find, similar to this:

 blocks = AppointmentBlock.find(:first,
          :conditions => ['employee_id = ? and (time_slots.start_at between ? and ? or time_slots.end_at between ? and ?)',
            self.employee_id, self.time_slot.start_at, self.time_slot.end_at,
            self.time_slot.start_at, self.time_slot.end_at],
          :joins => 'join time_slots on time_slots.time_slot_role_id = appointment_blocks.id')

这篇关于Rails:使用find方法访问连接表以获得多态关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-22 15:17