Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

foreign keys - Self-referential association in Ruby on Rails 6

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...