0

我有一个问题要解决,但不确定最合适的方法是什么来完成这项工作。这里开始的背景:

我正在使用程序和约会两种模型。Appointments 模型属于_Procedures 模型和Procedures 模型有_many Appointments。

现在在程序模型上,有两个关键点需要关注,而是两个关键列。

attr_accessible :visits, :occurence

访问是安排约会的具体次数。发生率是访问的频率。一个例子是visits: "5", occurence: "weekly"

因此,当我提交表单时,我想编写一个同时查看两者的方法,visits: "x"然后occurence: ["weekly", "biweekly", "monthly"]创建一个 if 或一个开关——php 确实会切换到 ruby​​ 版本——但我怀疑有一种优雅的方式来写这个向上。

我当前的创建方法如下所示:

def create
  @appointment = Appointment.new(params[:appointment])
  set_variables
  if @appointment.save
    flash[:success] = "Appointment scheduled!"
    redirect_to patient_path(@current_patient)
  else
    redirect_to patient_path(@current_patient)
    flash[:error] = "Appointment Date and Time cannot be blank, please try again."
  end
end

解决a)基于以下内容进行识别occurence: ["weekly", "biweekly", "monthly"]和处理的最佳方法是什么:visits: "x"

if @appointment.occurence == "weekly"
  (x-1).times do |n|
    submit same params but change within params appointment_date: = ((@appointment.appointment_date) + (n+1).week.to_formatted_s(:db)
    @appointment.save
  end
end

...依此类推,(n+1).month用于每月发生(n+2).day和每两周发生一次。

提前谢谢你,希望这能澄清事情。只需注意一项,我是否需要存储在数据库中visits:occurence:我怀疑不需要,但想确定在点击 models_controller 创建功能时使用它们。

4

2 回答 2

0

暂时解决了,相当粗糙——就像我目前的红宝石技能一样——但它似乎已经完成了这项工作。

     @appointment = Appointment.new(params[:appointment])
            set_variables
            @appointment.save
            if @procedure.occurence == "BIWEEKLY"
                    @visits = @procedure.visits - 1
                    @visits.times do |n|
                            procedure_id = @appointment.procedure_id
                            patient_id = @appointment.patient_id
                            appointment_date = (@appointment.appointment_date + 
                                    ((n+2)*2).days).to_formatted_s(:db)
                            appointment_time = @appointment.appointment_time
                            appointment_notes = @appointment.appointment_notes
                            attendance = "SCHEDULED"
                            @scheduled = Appointment.create(procedure_id: procedure_id, 
                                    patient_id: patient_id, appointment_date: appointment_date, 
                                    appointment_time: appointment_time, 
                                    appointment_notes: appointment_notes, attendance: attendance)
                    end
            end
            if @procedure.occurence == "WEEKLY"
                    @visits = @procedure.visits - 1
                    @visits.times do |n|
                            procedure_id = @appointment.procedure_id
                            patient_id = @appointment.patient_id
                            appointment_date = (@appointment.appointment_date + 
                                    (n+1).week).to_formatted_s(:db)
                            appointment_time = @appointment.appointment_time
                            appointment_notes = @appointment.appointment_notes
                            attendance = "SCHEDULED"
                            @scheduled = Appointment.create(procedure_id: procedure_id, 
                                    patient_id: patient_id, appointment_date: appointment_date, 
                                    appointment_time: appointment_time, 
                                    appointment_notes: appointment_notes, attendance: attendance)
                    end
            end
            if @procedure.occurence == "MONTHLY"
                    @visits = @procedure.visits - 1
                    @visits.times do |n|
                            procedure_id = @appointment.procedure_id
                            patient_id = @appointment.patient_id
                            appointment_date = (@appointment.appointment_date + (n+1).month).to_formatted_s(:db)
                            appointment_time = @appointment.appointment_time
                            appointment_notes = @appointment.appointment_notes
                            attendance = "SCHEDULED"
                            @scheduled = Appointment.create(procedure_id: procedure_id, 
                                    patient_id: patient_id, appointment_date: appointment_date, 
                                    appointment_time: appointment_time, 
                                    appointment_notes: appointment_notes, attendance: attendance)
                    end
            end
于 2013-03-01T08:54:37.180 回答
0

这是一个稍微不那么杂乱的解决方案,它应该可以满足您的需求,尽管它还假设您摆脱了该:appointment_date字段并更改:appointment_time为一个DateTime字段。有关DateTimes的更多信息,请查看:

(Stackoverflow 只允许我发布 2 个链接,因为我是 n00b,所以在您最喜欢的搜索引擎上搜索“DateTime Ruby”,以获取 Ruby 的文档和 DateTime 的 rails 方法)

将 DateTime 格式化为字符串以供查看:http ://apidock.com/ruby/DateTime/strftime

在表单中使用 DateTimes 的介绍:http: //guides.rubyonrails.org/form_helpers.html#using-date-and-time-form-helpers

@appointment = Appointment.new(params[:appointment])
set_variables
if @appointment.save 
    if @procedure.occurence == "WEEKLY"
       multiplier = 7
    elsif @procedure.occurence == "BIWEEKLY"
       multplier = 14
    else
       multiplier = 30
    end

    @visits = @procedure.visits - 1
    @visits.times do |n|
        Appointment.create!(
            :procedure_id => @appointment.procedure_id,
            :patient_id => @appointment.patient_id,
            :appointment_time => (@appointment.appointment_time + (multiplier * n).days),
            :attendance => "SCHEDULED"
        )
    end
else
    flash.now[:error] = "There appears to be an error, please try again."
    render 'new'
end
于 2013-05-22T17:44:32.950 回答