2014-04-01 35 views
0

請幫 我有兩個表:如何使2個表之間的關聯在Rails的

hospitals 
id | name | adress | main_doctor_id 

doctors 
id | name | contacts | bio | hospital_id 

在車型:

hospital.rb 
has_many :doctors 

doctor.rb 
belongs_to :hospital 

但我在一個需要更多的關聯 - 每個醫院都有一個首席醫生(main_doctor)。如何創建這些關聯以及如何從這個main_doctor的Doctors獲取數據? 紅寶石2.0.0p353 的Rails 4.0.2

+0

你說的「如何從醫生這個main_doctor數據」是什麼意思? –

回答

1

這是很簡單的搭建main_doctor協會:

class Hospital < ActiveRecord::Base 
    has_many :doctors 
    belongs_to :main_doctor, :class_name => 'Doctor' 
end 
+0

感謝您的建議。我怎樣才能獲得每家醫院的main_doctor數據? 'h = Hospital.find(1); h.main_doctor或h.main_doctor.name'不起作用 – TheDobriy

+0

@TheDobriy確保該醫院有其'main_doctor'。您在評論中顯示的內容是正確的。 –

+0

h.main_doctor.name NameError:未初始化的常量Company :: MainDoctor – TheDobriy

相關問題