Creating a table based on columns in ruby ​​on rails

I need a little help when creating a column based table dynamically instead of a row based table.

Let's say that I have many patients in the hospital and the hospital.

Hospital No. 1

          | Day 1 | Day 2 | Day 3     
Patient 1 | 36.6  | 36.4  | 36.5      
Patient 2 | 37.0  | 37.1  | 36.6      
Patient 3 | 37.1  | 36.4  | 36.7      
Patient 4 | 36.6  | 36.6  | 36.6      
Patient 5 | 36.7  | 37.1  | 36.4  

Every day, each patient checks the body temperature. I would like to receive a hint or a hint or an example of help dynamically drawing such a table - adding new data vertically, not horizontally. I hope you understand what I mean.

Thanks in advance:)

+3
source share
2 answers

I assume you meant something like this:

You have three models:

class Hostpital < ActiveRecord::Base
  has_many :patients
  has_many :temp_readings, :through => :patients
end

class Patient < ActiveRecord::Base
  belongs_to :hospital
  has_many :temp_readings
end

class TempReading < ActiveRecord::Base
  belongs_to :patient
end

Than you can create the table in the form of erb as follows:

<table>
  <thead>
    <tr>
      <th>&nbsp;</th>
      <%- some_hospital.temp_readings.map(&:date_of_reading).sort do |date_of_reading| -%>
        <th><%= date_of_reading %></th>
      <%- end -%>
    </tr>
  </thead>
  <tbody>
    <%- some_hospital.patients.each do |patient| -%>
      <tr>
        <th><%= patient.name %></th>
        <%- patient.hospital.temp_readings.map(&:date_of_reading).sort do |date_of_reading| -%>
          <td><%= patient.temp_reading.find_by_date_of_reading(date_of_reading) %></td>
        <%- end -%>
      </tr>
    <%- end -%>
  </tbody>
</table>

, , date_of_reading

+5

, :

class Patient < ActiveRecord::Base
  has_many :temp_readings
end

class TempReading < ActiveRecord::Base
  belongs_to :patient
end

temp_readings. (, 7 ) 7 temp_readings , .

0

Source: https://habr.com/ru/post/1752457/


All Articles