我目前正在做一个has_many:通过关联,但我不断收到此错误
NoMethodError:
undefined method `klass' for nil:NilClass
这是我联系班级的方式
模型: 病人
has_many :patient_templates
has_many :templates, through: :patient_template, dependent: :destroy
模板
has_many :patient_templates
has_many :patients, through: :patient_template, dependent: :destroy
Patient_template
belongs_to :patient
belongs_to :template
迁移
Patient_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