我有三个用户角色:
enum role: { staff: 0, clinician: 1, admin: 2 }
我在patients表中有一个user\u id列,用于存储创建病历的员工用户的id。为了提高名称的清晰度,我将列从user\u id重命名为author\u id,并调整了关系,我知道如何将更改引用到外键。
当我尝试访问/patients/new时,会出现错误:
unknown attribute 'user_id' for Patient.
这个错误特别突出了我的新患者方法中的这一行:
@patient = current_user.patients.build
我做错了什么?谢谢你的帮助!
患者表:
create_table "patients", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "age"
t.integer "staff_clinician_id"
t.integer "author_id"
t.index ["author_id"], name: "index_patients_on_author_id"
t.index ["staff_clinician_id"], name: "index_patients_on_staff_clinician_id"
end
患者模型
class Patient < ApplicationRecord
belongs_to :user, -> { where role: :staff }, foreign_key: 'author_id'
员工用户问题
require 'active_support/concern'
module StaffUser
extend ActiveSupport::Concern
included do
belongs_to :university
has_many :patients
has_many :referral_requests
validates :university_id, presence: true, if: :staff?
end
class_methods do
end
end
这是我的患者控制器:
class PatientsController < ApplicationController
before_action :require_login
def new
@patient = current_user.patients.build
end
def index
authorize Patient
@patients = policy_scope(Patient)
end
def show
@patient = Patient.find(params[:id])
end
def edit
@patient = Patient.find(params[:id])
end
def update
@patients = Patient.all
@patient = Patient.find(params[:id])
if @patient.update_attributes(patient_params)
flash[:success] = "Patient Updated!"
render 'patients/index'
else
render "edit"
end
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, :staff_clinician_id, :author_id, insurance_ids: [], gender_ids: [], concern_ids: [], race_ids: [])
end
end