我只想制作一个小连接表,最终存储关于该连接的额外信息(这就是我不使用 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
我的架构如下所示:
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
之前有人问过有关此示例的问题,但没有一个问题能解决我的情况。有任何想法吗?
谢谢,尼尔,Rails 新手。