实验室预计,得到了String

时间:2012-08-15 21:44:19

标签: ruby-on-rails forms foreign-keys associations

我是案例模型。案例模型有3个实际模型id的fiels 在案例模型

belongs_to :lab1, foreign_key: 'lab1', class_name: 'Lab', inverse_of: :lab1, conditions: {step1: true}
belongs_to :lab2, foreign_key: 'lab2', class_name: 'Lab', inverse_of: :lab2, conditions: {step2: true}
belongs_to :lab3, foreign_key: 'lab3', class_name: 'Lab', inverse_of: :lab3, conditions: {step3: true}
实验室模型中的

has_many :lab1, foreign_key: 'lab1', class_name: 'Case', inverse_of: :lab1
has_many :lab2, foreign_key: 'lab2', class_name: 'Case', inverse_of: :lab2
has_many :lab3, foreign_key: 'lab3', class_name: 'Case', inverse_of: :lab3

并且还形成了案件

 .lab_group
   = f.association :lab1, label: false, input_html: {'data-placeholder'=> t('step.one')}
   = f.input :lab1_delivered, label: t('case.done')
 .lab_group
   = f.association :lab2, label: false, input_html: {'data-placeholder'=> t('step.two')}
   = f.input :lab2_delivered, label: t('case.done')
.lab_group
   = f.association :lab3, label: false, input_html: {'data-placeholder'=> t('step.three')}
   = f.input :lab3_delivered, label: t('case.done')

但是当我尝试保存它时,我有一个错误

Lab(#70207759169540) expected, got String(#70207716712320)

并请求参数

{"utf8"=>"✓",
 "authenticity_token"=>"G0BWY6V6tkxRt81I0ZOxKXs8RD/7cdfbZfhVfRTmboo=",
 "case"=>{"number"=>"123",
 "doctor_id"=>"1",
 "lab1"=>"1",
 "lab1_delivered"=>"0",
 "lab2"=>"2",
 "lab2_delivered"=>"0",
 "lab3"=>"2",
 "lab3_delivered"=>"0",
 "delivered"=>"0",
 "patient"=>"",
 "comment"=>"for example"},
 "commit"=>"Save"}

和屏幕

我怎么解决它?

2 个答案:

答案 0 :(得分:1)

解决方案:
我的案例表中有lab1,lab2,lab3 colums。但是foreign_key没有看到它。我更改了lab1,lab2和lab3的名称,如lab1_id,lab2_id和lab3_id 我从关联中删除foreign_key 我们来看看。
在案例模型

belongs_to :lab1, class_name: 'Lab', inverse_of: :lab1, conditions: {step1: true}
belongs_to :lab2, class_name: 'Lab', inverse_of: :lab2, conditions: {step2: true}
belongs_to :lab3, class_name: 'Lab', inverse_of: :lab3, conditions: {step3: true}
实验室模型中的

has_many :lab1, class_name: 'Case', inverse_of: :lab1
has_many :lab2, class_name: 'Case', inverse_of: :lab2
has_many :lab3, class_name: 'Case', inverse_of: :lab3

并以表格

.lab_group
  = f.association :lab1, label: false, input_html: {'data-placeholder'=> t('step.one')}
  = f.input :lab1_delivered, label: t('case.done')
.lab_group
  = f.association :lab2, label: false, input_html: {'data-placeholder'=> t('step.two')}
  = f.input :lab2_delivered, label: t('case.done')
.lab_group
  = f.association :lab3, label: false, input_html: {'data-placeholder'=> t('step.three')}
  = f.input :lab3_delivered, label: t('case.done')

foreign_key不工作:(是不是错误?可能是。

答案 1 :(得分:0)

我认为您在这里使用名称lab1,lab2,lab3而不是lab1_id,lab2_id,lab3_id用于foreign_keys名称。