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
371 views
in Technique[技术] by (71.8m points)

relational database - Self referential association in Ruby on Rails

I have an app in which I have the "Patient" resource. I want to make a self association among patients, related to a viral infection. So, a patient can be an infectant, infected, or both. When I create a patient, I want to select which (another) patient infected him. So I added a "patient_id" field to "Patient". My code is the following:

The Patient model:

  class Patient < ApplicationRecord
      has_many :infected, class_name: 'Patient', foreign_key: :patient_id
      belongs_to :infectant, optional: true, class_name: 'Patient'
  end

The Patient form:

<div class="field">
      <%= form.label :patient_id %>
      <%= form.collection_select(:patient_id, Patient.all, :id, :document_number) %>
    </div>

  <div class="field">
    <%= form.label :first_name %>
    <%= form.text_field :first_name %>
  </div>

  <div class="field">
    <%= form.label :last_name %>
    <%= form.text_field :last_name %>
  </div>

  <div class="field">
    <%= form.label :document_number %>
    <%= form.number_field :document_number %>
  </div>

  <div class="field">
    <%= form.label :birth_date %>
    <%= form.datetime_select :birth_date %>
  </div>

  <div class="field">
    <%= form.label :province %>
    <%= form.number_field :province %>
  </div>

  <div class="field">
    <%= form.label :city %>
    <%= form.text_field :city %>
  </div>

  <div class="field">
    <%= form.label :status %>
    <%= form.number_field :status %>
  </div>

The Patient Index:

<table>
  <thead>
    <tr>
      <th>First name</th>
      <th>Last name</th>
      <th>Document number</th>
      <th>Birth date</th>
      <th>Province</th>
      <th>City</th>
      <th>Status</th>
      <th>Infectant</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @patients.each do |patient| %>
      <tr>
        <td><%= patient.first_name %></td>
        <td><%= patient.last_name %></td>
        <td><%= patient.document_number %></td>
        <td><%= patient.birth_date %></td>
        <td><%= patient.province %></td>
        <td><%= patient.city %></td>
        <td><%= patient.status %></td>
        <td><%= patient.patient_id %></td>
        <td><%= link_to 'Show', patient %></td>
        <td><%= link_to 'Edit', edit_patient_path(patient) %></td>
        <td><%= link_to 'Destroy', patient, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

With this Index, i can show the Id of the infectant. What I want, is to show in the Index the "document_number" of the infectant. But if instead of "patient.patient_id", I write "patient.infectant.document_number" but because "Patient" doesn′t have an "infected" field, it return error.

question from:https://stackoverflow.com/questions/65877183/self-referential-association-in-ruby-on-rails

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

1 Answer

0 votes
by (71.8m points)

One way to solve this is by using delegate:

class Patient < ApplicationRecord
  # ...
  delegate :document_number, 
     to: :infectant, 
     prefix: true,
     allow_nil: true
end

This will let you call:

<td><%= patient.infectant_document_number || "default value" %></td>

Without risking a nil error.


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

...