2017-09-25 49 views
0

我有在user.rb使用枚举定义角色的单个用户表:Rails的:问题assocating与用户记录谁没有创造这个纪录

enum role: { staff: 0, clinician: 1 } 

一位工作人员用户可以创建一个病人记录。创建患者记录的工作人员用户可能是该患者的临床医生,或者他们可能不是,在这种情况下,我有一个下拉表单,可为所有用户选择选项。 (临床医生用户角色是为外部临床医生 - 他们不参与)

我有一个病人表,其中我有user.id,我打算用它来存储创建病人的工作人员用户ID和staff_clinician_id ,其中I 打算用于存储患者医生的ID(其也将是职员用户 - 我知道困惑)。这里是我的患者模式:

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 "user_id" 
    t.index ["staff_clinician_id"], name: "index_patients_on_staff_clinician_id" 
    t.index ["user_id"], name: "index_patients_on_user_id" 

Then in my patients controller I've permitted staff_clinician_id and user_id: 

    def patient_params 
    params.require(:patient).permit(:age, :staff_clinician_id, :user_id, insurance_ids: [], gender_ids: [], concern_ids: [], race_ids: []) 

end 

,并在患者模型,我创建了这个关系:

has_one :staff_clinician, through: :users 

这里是我的形式:

<%= select_tag "staff_clinician_id", options_from_collection_for_select(User.where(role:"staff"), "id", "name"), prompt: "Select this patient's clinician" %> 

当我提出一个新的病人,服务器说:

Started POST "/patients" for ::1 at 2017-09-25 14:16:44 -0400 
Processing by PatientsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "patient"=>{"gender_ids"=>["1"], "race_ids"=>["1"], "insurance_ids"=>["1"], "concern_ids"=>["31"], "age"=>"243"}, "staff_clinician_id"=>"5", "commit"=>"Post"} 
    User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."remember_token" = ? ORDER BY "users"."id" ASC LIMIT ? [["remember_token", "3e607ec61e623710c58c42a0d313395439f82a82"], ["LIMIT", 1]] 
    Insurance Load (0.2ms) SELECT "insurances".* FROM "insurances" WHERE "insurances"."id" = 1 
    Gender Load (0.1ms) SELECT "genders".* FROM "genders" WHERE "genders"."id" = 1 
    Concern Load (0.2ms) SELECT "concerns".* FROM "concerns" WHERE "concerns"."id" = 31 
    Race Load (0.1ms) SELECT "races".* FROM "races" WHERE "races"."id" = 1 
    (0.0ms) begin transaction 
    Gender Exists (0.2ms) SELECT 1 AS one FROM "genders" WHERE "genders"."name" = ? AND ("genders"."id" != ?) LIMIT ? [["name", "Female"], ["id", 1], ["LIMIT", 1]] 
    Race Exists (0.1ms) SELECT 1 AS one FROM "races" WHERE "races"."name" = ? AND ("races"."id" != ?) LIMIT ? [["name", "American Indian or Alaska Native"], ["id", 1], ["LIMIT", 1]] 
    SQL (0.3ms) INSERT INTO "patients" ("created_at", "updated_at", "age", "user_id") VALUES (?, ?, ?, ?) [["created_at", 2017-09-25 18:16:44 UTC], ["updated_at", 2017-09-25 18:16:44 UTC], ["age", 243], ["user_id", 21]] 
    SQL (0.1ms) INSERT INTO "genders_patients" ("gender_id", "patient_id") VALUES (?, ?) [["gender_id", 1], ["patient_id", 7]] 
    Gender Exists (0.1ms) SELECT 1 AS one FROM "genders" WHERE "genders"."name" = ? AND ("genders"."id" != ?) LIMIT ? [["name", "Female"], ["id", 1], ["LIMIT", 1]] 
    SQL (0.1ms) INSERT INTO "concerns_patients" ("concern_id", "patient_id") VALUES (?, ?) [["concern_id", 31], ["patient_id", 7]] 
    SQL (0.1ms) INSERT INTO "insurances_patients" ("insurance_id", "patient_id") VALUES (?, ?) [["insurance_id", 1], ["patient_id", 7]] 
    SQL (0.2ms) INSERT INTO "patients_races" ("race_id", "patient_id") VALUES (?, ?) [["race_id", 1], ["patient_id", 7]] 
    Race Exists (0.1ms) SELECT 1 AS one FROM "races" WHERE "races"."name" = ? AND ("races"."id" != ?) LIMIT ? [["name", "American Indian or Alaska Native"], ["id", 1], ["LIMIT", 1]] 
    (10.5ms) commit transaction 
Redirected to http://localhost:3000/referral_requests/new?patient_id=7 
Completed 302 Found in 172ms (ActiveRecord: 17.5ms) 

但是当我在控制台中执行Patient.last时,它并未保存staff_clinician_id。它是零

我做错了什么?任何帮助感谢!

回答

2

您选择的标签应该命名为patient[staff_clinician_id],而不是staff_clinician_id

<%= select_tag "patient[staff_clinician_id]", options_from_collection_for_select(User.where(role:"staff"), "id", "name"), prompt: "Select this patient's clinician" %> 

如果使用基于对象的表单生成器,你可以使用速记:

<% form_for @patient do |f| %> 
    ... 
    <%= f.select :staff_clinician_id ... %> 
    ... 
<% end %> 

selectselect_tag非常不同的情况下被使用。

+0

谢谢!这工作。我是否也可以在Patient.last.staff_clinician这个实施方案中做些事情,或者这样做是不是按照设计工作?我可以做Patient.last.staff_clinician_id而不是Patient.last.staff_clinician或Patient.last.staff_clinician.name,这让我觉得关系配置不正确。 – mike9182

+0

没错,它应该是'has_one:staff_clinician','through'参数不是必须的。即使StaffClinician不是模型,也可以使用 –

+0

? – mike9182