Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

has_many :through NameError: uninitialized constant

I just want to make a little join table, eventually storing extra info on that join (which is why I'm not using HABTM). From the rails documentation of associations I've created the following models:

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

my schema looks like this:

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

When I'm in the console and I create a physician and patient instance:

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

And try to associate one to the other

@physician.patients << @patient

I get

NameError: uninitialized constant Physician::Patients

Questions about this example have been asked before but none have address my scenario. Any ideas?

Thanks, Neil, rails newbie.

like image 771
Neil Avatar asked Jan 15 '13 22:01

Neil


1 Answers

The belongs_to calls in your Appointment model should take a singular form, not a plural form:

class Appointment < ActiveRecord::Base
  belongs_to :physician
  belongs_to :patient
end
like image 54
Chris Salzberg Avatar answered Sep 21 '22 12:09

Chris Salzberg