【发布时间】:2017-07-14 00:19:53
【问题描述】:
这是我之前的一个问题的后续问题:How to Model doctor and patient relation 我对rails相当陌生,我正在病人和医生之间进行预约系统,我已经建立了关系,使用设计设置了身份验证。我有 3 个模型:
class Doctor < ApplicationRecord
has_many :appointments
has_many :patients, through: :appointments
end
class Patient < ApplicationRecord
has_many :appointments
has_many :doctors, through:appointments
end
class Appointment < ApplicationRecord
#table_columns: id | start_time| end_time| doctor_id| patient_id|slot_taken|
belongs_to :patient
belongs_to :doctor
end
我已经创建了约会控制器:一些操作如下:
#current_doctor comes from devise. Have created two separate models for doctor and patients using devise
def create
@appointment = current_doctor.appointments.build(appointment_params)
respond_to do |format|
if @appointment.save
format.html { redirect_to appointments_path, notice: 'Appointment was successfully created.' }
else
format.html { render :new }
end
end
end
def update
respond_to do |format|
if @appointment.update(appointment_params)
format.html { redirect_to appointments_path, notice: 'Appointment was successfully updated.' }
else
format.html { render :edit }
end
end
end
private
def appointment_params
params.require(:meeting).permit(start_time, end_time)
end
如您所见,医生可以创建他/她有空的时间段,并且可以为他/她自己编辑、更新和销毁这些时间段,当他创建一个时隙时,预约表会更新为医生 ID、开始时间和结束时间.
现在,我应该怎么做才能添加 patient_id 并将 slot_taken 更改为 true(默认值:false)?当患者预订可用时隙时,约会表应使用 Patient_id 和 slot_taken 值进行更新。我应该如何更新约会表,我应该把代码放在哪里?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-5