Rails:将记录与未创建该记录的用户相关联的问题

时间:2017-09-25 18:23:58

标签: ruby-on-rails

我有一个Users表,其中使用user.rb中的枚举定义了角色:

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

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

我有一个患者表,其中我有user.id,我打算用它来存储创建患者的员工用户ID,以及staff_clinician_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

在Patient模型中我创建了这种关系:

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。它是零

我做错了什么?任何帮助表示赞赏!

1 个答案:

答案 0 :(得分: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用于非常不同的上下文。