これは私の以前の質問からのフォローアップの質問です:How to Model doctor and patient relation 私はかなりレールに新しく、私は患者と医者の間の予約予約システムを作っている、私は関係を作って、 I 3つのモデルがあります:私は予定コントローラを作成しているhas_many:throughテーブルを更新するには?
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
あなたが見ることができるように、医師は、彼が利用可能になったときのタイム・スロットを作成することができ、かつ彼/彼女自身のために同じものを編集、更新、破壊することができ、彼が作成したときには、予定表はdoctor_id、start_time、end_timeで更新されます。
ここで、patient_idとchange slot_takenをtrue(デフォルト:false)に追加するにはどうすればよいですか?患者が利用可能なスロットを予約するとき、appointmentsテーブルはpatient_idとslot_takenの値で更新する必要があります。予定表を更新する方法と、同じコードをどこに置くべきですか?
例えば者は、医師Aがあるとしようと、彼は9時AMに7月13日08:00で利用可能な時間帯を作成しました。今度は患者の側で、患者は、医者Aが7月13日8:00 AMから09:00 AMに利用可能であることを見ることができる。彼はブックボタンをクリックして、patient_idが更新されるはずです。そして私はbook_idにbookボタンを渡しています。今、私の質問は、私はそれを同じアップデートメソッドにアポイントテーブル内で渡すべきですか? – ln7373
その更新メソッドを再利用することができます。私があなたに提示したフォームにはすでにdoctor_id、start_time、end_timeの情報が含まれていると思いますので、取得する必要があるのはpatient_id情報だけです。更新メソッド内で取得できます。 current_user(あなたの場合はcurrent_patient) –
ありがとう。それは今働く。私はそれが良い練習かどうか、または更新メソッドがそれに似たアクセスであるかどうかはわかりませんでした。あなたが提案したように私は1つのモデルにも変更します。 – ln7373