I am creating an application for managing Covid-19 cases, and to track the root of major infections, every patient that is loaded into the system, can have an infectant. So, I have the Patient resourse, which moreover it′s own "id" field, it has a "patient_id" field thet references who might have infected him. This possible ifectant can be unknown, so I want to have the possibility of leaving "patient_id" blank. I tried the following in the patient model:
class Patient < ApplicationRecord
has_many :infected, class_name: 'Patient', foreign_key: :patient_id
belongs_to :infectant, optional: true, class_name: 'Patient', foreign_key: :patient_id
end
I also modified the migration so it says patient_id, null: true, but it isn′t working.
question from:
https://stackoverflow.com/questions/65852041/self-referential-association-in-ruby-on-rails-6 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…