2013-01-15 8 views
9

Po prostu chcę zrobić małą tabelę dołączenia, w końcu przechowując dodatkowe informacje o tym sprzężeniu (dlatego nie używam HABTM). Z dokumentacji szynach stowarzyszeń Utworzyłem następujących modeli:has_many: through NameError: niezainicjowana stała

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 

mój schemat wygląda tak:

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 

Kiedy jestem w konsoli i tworzę z lekarzem i pacjentem instancji:

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

I spróbuj skojarzyć jednego do drugiego

@physician.patients << @patient 

uzyskać

NameError: uninitialized constant Physician::Patients 

Pytania o tym przykładzie został poproszony wcześniej, ale nikt nie ma rozwiązać mój scenariusz. Jakieś pomysły?

Dzięki, Neil, szyny newbie.

+0

Potencjalny duplikat: http://stackoverflow.com/questions/4415446/adding-and-removing-from -a-ma-wiele-do-relacji –

Odpowiedz

17

W belongs_to wzywa w modelu Appointment powinien wziąć formy pojedynczej, a nie mnogiej:

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

Dzięki kolego. Bardzo doceniane! – Neil

Powiązane problemy