4

I need a bit help with dynamically generating column-based table instead of row-based table.

Let's say that I have a hospital and hospital has many patients.

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  

Each day, each patient has his body temperature checked. I would like to get a tip/hint or example of help dynamically drawing such table - adding new data vertically, not horizontally. Hopefully you get what i mean.

Thank you, in advance :)

2
  • what do you mean by dynamically drawing such table. Do want to make something with ajax? Right now your question has nothing to do with RoR Commented Jun 30, 2010 at 13:13
  • First, thanks for editing my post, that table is looking like a table now. "Dynamically drawing such table" may be indeed unfortunate wording. I just want to iterate through a collection. I need a suggestion how to prepare such collection to iterate through it in a view, in a column-based style. Commented Jun 30, 2010 at 13:21

2 Answers 2

5

I'm assuming 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 build a table in an erb view like this:

<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>

I'm assuming you have some column with a date in your Reading Model, I just called it date_of_reading

Sign up to request clarification or add additional context in comments.

1 Comment

You sir, are brilliant! The view is what have been problematic to me. It needed a few corrections, but anyone who will find this topic will know how to help himself. Many, many thanks.
0

If your question relates to the storage and representation of the data within your application then you can do this with 2 models as follows:

class Patient < ActiveRecord::Base
  has_many :temp_readings
end

class TempReading < ActiveRecord::Base
  belongs_to :patient
end

When creating a new reading for each patient you add a row to the temp_readings table. When displaying these readings (say for the last 7 days) you print the last 7 row values of temp_readings for each patient across a single line giving you the columnar output.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.