Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 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 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...