我需要在表中显示多维数组数据,如下所示:
对于 sla.expected_billing,左侧有一个包含 12 个周期的网格(行)
顶部有 7 个分类帐编号(列)
适用字段中的金额,但可能有空值
例如,
第 1 期:分类帐 1 = 400.00,分类帐 2 = 空,分类帐 3 = 250.55,分类帐 4 = 500,等等。
时期 2:分类帐 1 = 空,分类帐 2 = 空,等等...
class ExpectedBilling < ActiveRecord::Base
belongs_to :sla
belongs_to :period
belongs_to :ledger
end
class Period < ActiveRecord::Base
has_many :expected_billings
end
class Ledger < ActiveRecord::Base
has_many :expected_billings
end
class Sla < ActiveRecord::Base
has_many :expected_billings
end
我在想 Matrix 库和/或 Class.transpose 可能是我需要的,但我不确定。当任何给定数组中可能存在空值时,恐怕它也不会正确填充网格。
任何人都可以指出我正确的方向吗?
最佳答案
我会稍微不同地对其进行建模:
class Sla < ActiveRecord::Base
has_one :expected_billing
end
class ExpectedBilling < ActiveRecord::Base
has_many :periods
belongs_to :sla
end
class Period < ActiveRecord::Base
has_many :ledgers
belongs_to :expected_billing
end
class Ledger < ActiveRecord::Base
belongs_to :period
end
要在 View 中打印表格:
<table>
<!-- insert header row here -->
<% sla.expected_billing.periods.each do |period| %>
<tr>
<!-- insert header column here -->
<% period.ledgers.each do |ledger| %>
<td><%= ledger.value %></td>
<% end %>
</tr>
<% end %>
</table>
关于ruby-on-rails - rails 3 : Matrix Grid View,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6866130/