ruby on rails 3 - has_many:through NameError:未初始化常量



我只想创建一个小连接表,最终在该连接上存储额外的信息(这就是我不使用HABTM的原因)。从关联的rails文档中,我创建了以下模型:

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

我的schema是这样的:

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
关于这个例子的问题以前有人问过,但没有一个针对我的场景。什么好主意吗?

谢谢,尼尔,铁路新手。

您的Appointment模型中的belongs_to调用应该采用单数形式,而不是复数形式:

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

相关内容

  • 没有找到相关文章

最新更新