2013-01-15 45 views
9

我只是想做一個小表連接表,最終存儲該連接的額外信息(這就是爲什麼我沒有使用HABTM)。從協會的軌道文檔我創建了以下型號:has_many:通過NameError:未初始化的常量

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

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

class Appointment < ActiveRecord::Base 
    belongs_to :physicians 
    belongs_to :patients 
end 

我的模式是這樣的:

ActiveRecord::Schema.define(:version => 20130115211859) do 

    create_table "appointments", :force => true do |t| 
    t.datetime "date" 
    t.datetime "created_at", :null => false 
    t.datetime "updated_at", :null => false 
    t.integer "patient_id" 
    t.integer "physician_id" 
    end 

    create_table "patients", :force => true do |t| 
    t.string "name" 
    t.datetime "created_at", :null => false 
    t.datetime "updated_at", :null => false 
    end 

    create_table "physicians", :force => true do |t| 
    t.string "name" 
    t.datetime "created_at", :null => false 
    t.datetime "updated_at", :null => false 
    end 

end 

當我在控制檯和我創建了一個醫生和患者的實例:

@patient = Patient.create! 
@physician = Physician.create! 

並嘗試一個關聯到其他

@physician.patients << @patient 

我得到

NameError: uninitialized constant Physician::Patients 

這個例子問題之前,已要求但沒有解決我的方案。有任何想法嗎?

謝謝, Neil,rails新手。

+0

可能重複:http://stackoverflow.com/questions/4415446/adding-and-removing-from -a-have-many-through-relation –

回答

17

Appointment模型belongs_to調用應該採取單數形式,沒有複數形式:

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

感謝隊友。非常感激! – Neil

相關問題