Basic has_one association nil:class error upon reflection

I’m working on an app written in 2018 on rails 6.0 and after upgrading to 7 I’ve been getting an error: "NoMethodError: undefined method `klass’ for nil:NilClass (NoMethodError)

At first i blamed Administrate because all the rest of my index views worked except one but then i opened rails console and pulled them all, (like an index query) which worked. but when getting attributes of the individual objects, the assoications with all but one class_name work. they all work under Rails 6 but not rails 7 or 8.

to be clear, here’s a snippit of my “JobPosting” class. the one in question

belongs_to :job_coach, class_name: ‘User’, optional: true

belongs_to :mentor, class_name: ‘Contact’, optional: true

now, the object has a valid job_coach_id and mentor_id but if i go

email_writer = JobPosting.find(3)

email_writer.mentor

i get

Contact:0x000000010e58ba60 … which is fine, but when i go

email_writer.job_coach

I get this error:

NoMethodError: undefined method `klass’ for nil:NilClass (NoMethodError)

    through_reflection.klass.\_reflect_on_association(n)

and I have no idea why. Other associations with of other classes are fine, it’s only the User class that can’t be referenced.

Hard to follow exactly what is going on here.

So in the Contact model do you have a has_one that goes to JobPosting?

class Contact < ApplicationRecord
  has_one :job_posting, foreign_key: :mentor_id
end

Or … perhaps does User have has_one :contact through: :job_postings?

Reason I ask is it’s hard to tell how you are getting a message about “through_reflection.klass._reflect_on_association”.