0

I have a model called Patient and table patients

This model Patient represents a patient in a doctor's office.

I have a model for gender populated with different gender names

Patient has_and_belongs_to_many :genders and Gender has_and_belongs_to_many :patients.

I have a similar implementation for race, concerns, insurances.

In a Patients index view I'm trying to show a given patients race or races, concern or concerns, insurance or insurances (they could have none, 1, or many). How can I show all of them all at once? The only way I have been able to figure out to show any is to call first.name as seen below. Thanks!

<table>
  <tr>
      <th>Patient ID</th>
      <th>Created</th>
      <th>Created By</th>
      <th>Staff Clinician</th>
      <th>Concerns</th>
      <th>Gender</th>
      <th>Race</th>
      <th>Insurance</th>
      <th>Referral Request</th>
  </tr>

<% @patients.each do |patient| %>
  <tr>
    <td><%= patient.id %></td>
    <td><%= patient.created_at.strftime("%Y-%m-%d") %></td>
    <td><%= patient.user_id %></td>
    <td><%= patient.staff_clinician_id %></td>
    <td><%= patient.concerns.first.name %></td>
    <td><%= patient.genders.first.name %></td>
    <td><%= patient.races.first.name %></td>
    <td><%= patient.insurances.first.name %></td>
    <td><%= link_to "Show", patient %></td>
    <td><%= link_to "Edit", edit_patient_path(patient) %></td>
    <td><%= link_to "Remove", patient, method: :delete, data: { confirm: "Are you sure?" } %></td>
  </tr>
<% end %>
</table>
mike9182
  • 269
  • 1
  • 3
  • 17

2 Answers2

1

You can loop over each model instance, for example:

<% patient.concerns.each do |concern| %>
    <td><%= concern.name %></td>
<% end %>
jon1467
  • 89
  • 2
  • 7
1

You can use to_sentence over the collections.

<td><%= patient.concerns.map(&:name).to_sentence %></td>

or

<td><%= patient.concerns.map{ |concern| concern.name&.titleize }.to_sentence %></td>

The & is for safe navigation, meaning if the concern does not have a name then the following .titleize won't blow up.

Anyway, this approach of displaying the collection is nice when working with tables because only need one cell to display everything.

abax
  • 727
  • 5
  • 9
  • Thanks abax - is there a '.' out of place in your second example? I get a syntax error when I try to insert it in my code. – mike9182 Sep 26 '17 at 14:30
  • does it work without the `&` just using `name.titleize`? The safe navigation operator was added in Ruby 2.3.0. Might depend on your Ruby version. I can only guess without knowing the specific error. Perhaps you could post it in a comment. – abax Sep 26 '17 at 16:03