Rails 4 - SimpleForm关联不创建记录

时间:2016-04-05 18:43:06

标签: ruby-on-rails ruby-on-rails-4 associations simple-form simple-form-for

应用程序/模型/ encounter.rb

class Encounter < ActiveRecord::Base
  has_many: orders
end

应用程序/模型/ order.rb

class Order < ActiveRecord::Base
  belongs_to :encounter
  has_many :order_tests
  has_many :tests, through :order_tests
end

应用程序/模型/ order_test.rb

Class OrderTest < ActiveRecord::Base
  belongs_to :order
  belongs_to :test
end

应用程序/模型/ test.rb

Class Test < ActiveRecord::Base
  has_many :order_tests
  has_many :orders, through: :order_tests
end

我正在尝试将多个测试添加到给定遭遇的第一个顺序。我试图通过使用simple_form关联来实现这一点。这是我在视图中的设置

= simple_form_for @encounter do |f|
  = f.input :name
  = f.patient_id
  = f.doctor_id
  = simple_form_for @encounter.orders.first do |o|
    = o.associations tests, as: :check_boxes, collection: Test.all

这是我的服务器日志

Started PATCH "/encounters/71?next_step=Meds+Entry&step_group=accessioning&task=326" for 127.0.0.1 at 2016-04-05 13:10:50 -0500
Processing by EncountersController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"qSC7IHInq0fqArDXsXbKENSMYM/5B1Ijry4hZ1o6oVtyVb14ECnt95rNX2Pqv+GHmanugkBvQfc686JmKJeSeA==", "encounter"=>{"patient_id"=>"", "organization_id"=>"", "address_id"=>"", "name"=>"", "encounter_type_id"=>"3", "encounter_at"=>"", "do_next_step"=>"0"}, "patient_first_name"=>"", "patient_middle_name"=>"", "patient_last_name"=>"", "patient_dob"=>"", "patient_gender"=>"", "patient_race"=>"", "patient_ssn"=>"", "address"=>"", "city"=>"", "state"=>"", "zip"=>"", "npi"=>"", "order"=>{"test_ids"=>["1", "2", "3", ""]}, "button"=>"", "next_step"=>"Meds Entry", "step_group"=>"accessioning", "task"=>"326", "id"=>"71"}
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 2]]
  CACHE (0.0ms)  SELECT  "encounters".* FROM "encounters" WHERE "encounters"."id" = $1 LIMIT 1  [["id", "71"]]
  Task Load (0.2ms)  SELECT  "tasks".* FROM "tasks" WHERE "tasks"."deleted_at" IS NULL AND "tasks"."id" = $1 LIMIT 1  [["id", 326]]
   (0.2ms)  BEGIN
   (0.2ms)  COMMIT
   (0.2ms)  BEGIN
   (0.2ms)  COMMIT
Redirected to http://localhost:3002/encounters/71
Completed 302 Found in 45ms (ActiveRecord: 4.5ms)

好像它没有添加任何记录,我有点不确定如何解决这个问题。我正在尝试在 order_tests 表(order_id,test_id)中为每个已选中的复选框添加新记录。

1 个答案:

答案 0 :(得分:0)

假设日志剪辑用于更新遭遇,则存在一个明显的问题,即没有调用更新命令...来自我正在处理的项目中的随机示例:

  SQL (22.7ms)  UPDATE "users" SET "trust_name" = $1, "updated_at" = $2 WHERE "users"."id" = $3  [["trust_name", "ttrusdt dfsdf"], ["updated_at", "2016-04-05 19:03:26.823849"], ["id", 24]]

这让我相信控制器操作中发生了意外情况...发布代码可能会有所帮助。

如果它在其他地方失败,比如验证,我希望在日志中看到更新命令,然后回滚...拒绝更新。