gpt4 book ai didi

ruby-on-rails - 这个错误是什么意思?未定义方法 `klass' 为 nil :NilClass

转载 作者:数据小太阳 更新时间:2023-10-29 08:57:08 24 4
gpt4 key购买 nike

我目前正在做一个 has_many :through association,但我一直收到这个错误

   NoMethodError:
undefined method `klass' for nil:NilClass

这就是我关联类(class)的方式

型号: 病人

   has_many :patient_templates
has_many :templates, through: :patient_template, dependent: :destroy

模板

   has_many :patient_templates
has_many :patients, through: :patient_template, dependent: :destroy

患者模板

   belongs_to :patient
belongs_to :template

迁移

患者模板

   def change
create_table :patient_templates do |t|
t.datetime :delivery
t.belongs_to :patient, index: true
t.belongs_to :template, index: true

t.timestamps null: false
end
end

我做错了什么?

最佳答案

你打错了。您需要通过关联将您的 has_many 复数化,如下所示:

模板:

has_many :patients, through: :patient_templates, dependent: :destroy

病人

has_many :templates, through: :patient_templates, dependent: :destroy

关于ruby-on-rails - 这个错误是什么意思?未定义方法 `klass' 为 nil :NilClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49770871/

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