【发布时间】:2017-05-31 06:48:28
【问题描述】:
我正在尝试创建一个表格,该表格主要按日期显示每个字段的数字。这个我只有一个模型
查看:
<table class="table-table-verif">
<thead class="fixed_headers">
<tr id = "thead-title">
<th>Date</th>
<th># True Positive</th>
<th># True Negative</th>
<th># False Positive</th>
<th># False Negative</th>
</tr>
</thead>
<tbody>
<% @verification.each do |s| %>
<tr id="thead-value">
// what to do here?
</tr>
<% end %>
</tbody>
</table>
我想在我的控制器中调用这些查询。
控制器:
def date
@verification = Verification.group(:Date).where("Findings = ? or Findings = ?", 'False Positive','False Negative').order(Date: :asc).count
end
def number_true_positive
Verification.group(Date).where("Findings = ?", 'True Positive').order(Date: :asc).count
end
def number_false_positive
Verification.group(:Date).where("Findings = ?", 'False Positive').order(Date: :asc).count
end
def number_true_negative
Verification.group(:Date).where("Findings = ?", 'True Negative').order(Date: :asc).count
end
def number_false_negative
Verification.group(:Date).where("Findings = ?", 'False Negative').order(Date: :asc).count
end
每一列都要统计各自记录的数量,并根据它们的日期将数字放在各自的<th>上。
有什么更好的方法?
【问题讨论】:
-
将这些查询写入
helper方法。 -
Controller 是放置那些 methods 的好地方。您应该将它们移至 model。
-
如果你只需要计数,为什么还要
ordering呢? -
@Md.FarhanMemon 因为我使用的是 .group,我想根据日期和升序显示结果。
-
@Emu 我将如何实现这个?我的意思是如何调用辅助方法来查看视图?
标签: html mysql ruby-on-rails ruby controller