2011-09-19 34 views
0

考慮這些作爲我的表結構如何在rails的關係表中的額外列中插入值?

class Physician < ActiveRecord::Base 
    has_many :appointments 
    has_many :patients, :through => :appointments 
end 

class Appointment < ActiveRecord::Base 
    belongs_to :physician 
    belongs_to :patient 
end 

class Patient < ActiveRecord::Base 
    has_many :appointments 
    has_many :physicians, :through => :appointments 
end 

Appointments表有一個名爲relationship一個額外的列。每次當我將醫師和患者聯繫起來時,我想添加一種關係[我的例子聽起來很奇怪,我如何在這個列中插入值?

回答

0

爲什麼不是這樣?

patient = Patient.create(:name => 'Humpty Dumpty') 
physician = Physician.create(:name => "All the King's horses and all the King's men") 
appointment = Appointment.create(:patient => patient, :physician => physician, :relationship => 'surgeon') 
+0

沒有。那也不管用。我收到一個錯誤。 – Rahul

+0

你可以展示你有什麼?當然,上面的代碼會給出錯誤,這只是一個例子。你必須適應你所擁有的東西,但是你沒有展示你的東西。 –

+0

我創建了適當的對象。如果我採用上述樣本,我可以創建患者和醫生對象,但是當我嘗試創建約會時,我得到一個錯誤,說'關係不能爲空',而不管我給':relationship =>'General'同時創建我的約會對象。 – Rahul

相關問題