2012-08-15 51 views
0

我heve案例模型。案例模型有3個實驗室模型id。
案例模型實驗室預計,得到字符串

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} 
在Lab模式

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"} 

和屏幕

我怎樣才能解決呢?

+0

你應該表現出該錯誤所指代碼:**您的「創建」操作** – deefour 2012-08-15 22:02:58

回答

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不工作:(是不是錯誤?可能是。

0

我認爲你在這裏使用的名稱是lab1,lab2,lab3,而不是lab1_id,lab2_id,lab3_id是用於foreign_keys的名稱。

+0

不,我在病例表中有lab1列。我猜我怎麼解決它。我現在寫。 – achempion 2012-08-15 22:15:25