2010-06-30 46 views
3

我需要幫助動態生成基於列的表而不是基於行的表。在軌道視圖中生成基於列的表ruby

假設我有一家醫院和醫院有很多患者。

醫院沒有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 

每一天,每一個病人他的體溫檢查。我想獲得一個提示/提示或動態繪製這樣的表格的示例 - 垂直添加新數據,而不是水平添加。希望你明白我的意思。

謝謝提前:)

+0

什麼你的意思是'動態繪製這樣的表格'。想用Ajax做點什麼嗎?現在您的問題與RoR – jigfox 2010-06-30 13:13:24

+0

無關首先,感謝您編輯我的帖子,該桌現在看起來像一張桌子。 「動態畫這樣的桌子」可能確實是不幸的措辭。我只是想遍歷一個集合。我需要一個建議,如何準備這樣的集合,以基於列的風格在視圖中遍歷它。 – socjopata 2010-06-30 13:21:19

回答

5

我假設你的意思是這樣的:

你有三種型號:

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 

比你可以在這樣的ERB視圖建一個表:

<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

+0

您先生,精彩!這個觀點對我來說是有問題的。它需要一些更正,但是任何會找到這個主題的人都會知道如何幫助自己。非常感謝。 – socjopata 2010-07-01 07:37:13

0

如果您的問題涉及到你的應用程序中的數據的存儲和代表性,那麼你可以按照如下用2種機型做到這一點:

class Patient < ActiveRecord::Base 
    has_many :temp_readings 
end 

class TempReading < ActiveRecord::Base 
    belongs_to :patient 
end 

當創建爲每位患者添加一行到temp_readings表的新讀數。當顯示這些讀數時(例如最近7天),您可以在單行中爲每位患者打印temp_readings的最後7行值,併爲您提供柱狀輸出。