HKD
HKD

Reputation: 31

What does this error mean? undefined method `klass' for nil:NilClass

I'm currently doing a has_many :through association, and I keep getting this error

   NoMethodError:
   undefined method `klass' for nil:NilClass

This is the way I relate my classes

MODELS: Patient

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

Template

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

Patient_template

   belongs_to :patient
   belongs_to :template

MIGRATION

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

What am I doing wrong?

Upvotes: 2

Views: 2161

Answers (1)

Cyzanfar
Cyzanfar

Reputation: 7146

You have a typo. You will need to pluralize your has_many through association like so:

template:

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

Patient

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

Upvotes: 2

Related Questions