gpt4 book ai didi

ruby-on-rails-3 - has_many :through NameError: uninitialized constant

转载 作者:行者123 更新时间:2023-12-04 03:08:37 26 4
gpt4 key购买 nike

我只想制作一个小的连接表,最终存储有关该连接的额外信息(这就是为什么我不使用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

之前已经询问过有关此示例的问题,但没有一个解决我的情况。有任何想法吗?

谢谢,
尼尔,新手。

最佳答案

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

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

关于ruby-on-rails-3 - has_many :through NameError: uninitialized constant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14347828/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com