Activerecord Rails 3路使用多个if语句和表单create/new

Activerecord Rails 3路使用多个if语句和表单create/new,activerecord,if-statement,ruby-on-rails-3.2,multiple-instances,Activerecord,If Statement,Ruby On Rails 3.2,Multiple Instances,我有一个问题要处理,不确定什么是最合适的方法将使这项工作。以下是开始的背景: 我正在处理两种模式的程序和预约。预约模型属于程序模型,程序模型有许多预约 现在关于过程模型,有两个关键点需要关注,而不是两个关键列 attr\u可访问:访问:发生 访问是安排约会的具体次数。 发生率是指访问的频率。例如,访问:“5次”,发生时间:“每周” 因此,当我提交表单时,我想写一个方法,同时查看访问:“x”和发生:[“每周”、“两周”、“每月”],然后创建一个if或一个开关——php确实会切换到ruby版本——但

我有一个问题要处理,不确定什么是最合适的方法将使这项工作。以下是开始的背景:

我正在处理两种模式的程序和预约。预约模型属于程序模型,程序模型有许多预约

现在关于过程模型,有两个关键点需要关注,而不是两个关键列

attr\u可访问:访问:发生

访问是安排约会的具体次数。 发生率是指访问的频率。例如,
访问:“5次”,发生时间:“每周”

因此,当我提交表单时,我想写一个方法,同时查看
访问:“x”
发生:[“每周”、“两周”、“每月”]
,然后创建一个if或一个开关——php确实会切换到ruby版本——但我怀疑有一个优雅的方法来编写它

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

def创建
@约会=约会。新建(参数[:约会])
设置变量
如果@appointment.save
flash[:success]=“预约已安排!”
重定向到患者路径(@current\u patient)
其他的
重定向到患者路径(@current\u patient)
flash[:error]=“约会日期和时间不能为空,请重试。”
结束
结束
解决以下问题的最佳方法是:a)识别
事件:[“每周”、“两周”、“每月”]
,然后根据类似以下内容处理
访问:“x”

if@appointment.occurrence==“每周”
(x-1)。时间是多少|
提交相同的参数,但在参数内更改约会日期:=(@appointment.appointment\u date)+(n+1).week.to\u格式的日期(:db)
@预约。保存
结束
结束
…以此类推,使用
(n+1).month
表示每月发生
(n+2).天,以及每两周发生一次


提前谢谢你,希望这能澄清一些事情。只需注意一点,我是否需要将它们存储在数据库中
访问:
发生:
,我怀疑,但我想确定它们是在点击models\u controller create函数时使用的。

目前解决了,相当粗糙——这是我当前的ruby技能集——但它确实是似乎已经完成了任务。


这里有一个稍微不那么杂乱的解决方案,可以满足您的需要,不过它也假设您去掉了
:约会日期
字段,并将
:约会时间
更改为
日期时间
字段。有关日期时间的更多信息,请查看:

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

将视图的DateTime格式化为字符串:

关于在表单中使用日期时间的介绍:


我怀疑有一种更干净的方式来编写/呈现这段代码,但这正是目前对我有效的方式。欢迎评论。非常感谢批评!(这将有助于我改进更干净的代码)这是一个可爱且受欢迎的变化!感谢您花时间来编写这段代码。
     @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
@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