0

我有一個問題的方法,並不知道什麼是最合適的方法將使這項工作。這裏的背景開始:Rails的3種方式與多個if語句和表單創建/新

有兩種模型我正在使用程序和約會。約會模型屬於程序模型和過程模型has_many約會。

現在在過程模型中有兩個關鍵點需要關注,而兩個關鍵點。

attr_accessible :visits, :occurence

參觀是次安排的約會(一個或多個)的具體數目。 發生次數是訪問次數。一個例子是visits: "5", occurence: "weekly"

所以,當我提出我的形式,我想編寫一個看起來在兩個visits: "x"occurence: ["weekly", "biweekly", "monthly"]來然後創建一個if或開關的方法 - PHP並切換仍在調查紅寶石版本 - 但我懷疑有一個優雅的方式來寫這個。

我現在的建立方法是這樣的:

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 

什麼是解決一個識別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創建函數時使用的。

回答

0

這裏有一個稍微不那麼雜亂的解決方案,它應該做你需要什麼,儘管它也假定您擺脫:appointment_date領域,改變:appointment_timeDateTime場。有關DateTime是否簽出更多的信息:

(將#1只允許我發佈2個鏈接,因爲我是一個的n00b所以搜索「日期時間紅寶石」您最喜愛的搜索引擎爲Ruby文檔和軌道的方法DATETIME)

格式化的DateTime到字符串的觀點:http://apidock.com/ruby/DateTime/strftime

介紹在窗體中使用DateTime是否: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 
+0

這是一個可愛且值得歡迎的改變!感謝您花時間寫這篇文章。 – sandovalg 2013-06-03 06:52:52

0

現在解決,相當粗糙 - 就像我目前的ruby技能集 - 但它似乎已經完成了這項工作。

 @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 
+0

我懷疑有一個更清潔的WA y寫/提出這段代碼,但這是目前爲我工作的。歡迎評論。批評是非常感謝! (將有助於我改進更乾淨的代碼) – sandovalg 2013-03-01 08:56:05