【发布时间】:2017-08-31 19:40:02
【问题描述】:
我有一个用户模型和一个患者模型。患者不是该应用程序的用户。用户本质上是创建患者记录的工作人员。在某些情况下,创建患者记录的用户也是该患者的医生。在其他情况下,患者的医生可能是单独的用户。
我想将患者医生的用户 ID 保存到患者模型,而不是碰巧创建患者的用户。我想象的实现是,我将在表单中有一个下拉字段供用户选择患者的医生,包括选择自己的选项。我怎样才能做到这一点?我什至以正确的方式思考这个问题吗?这是我当前的实现:
class Patient < ApplicationRecord
belongs_to :user
class User < ApplicationRecord
has_many :patients
病人控制者
类患者控制器 患者的架构: 我有两个角色:一个是工作人员,一个是临床医生。员工用户将是创造患者的人。创建患者记录的员工用户可能是也可能不是该特定患者的医生。def new
@patient = current_user.patients.build
end
def create
@patient = current_user.patients.build(patient_params)
if @patient.save
flash[:success] = "Patient Created!"
redirect_to new_referral_request_path(patient_id: @patient.id)
else
Rails.logger.info(@patient.errors.inspect)
render 'patients/new'
end
end
private
def patient_params
params.require(:patient).permit(:age, :user_id, insurance_ids: [], gender_ids: [], concern_ids: [], race_ids: [])
end
end
create_table "patients", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "age"
t.string "user_id"
t.index ["user_id"], name: "index_patients_on_user_id"
end
class User < ApplicationRecord
self.inheritance_column = :role
enum role: { Staff: 0, Clinician: 1}
【问题讨论】:
标签: ruby-on-rails