我的控制器:
def index
@unique_bug = Rating.find_by_sql("SELECT bug FROM ratings WHERE bug <> '' GROUP BY bug")
end
评级模型:
def self.metrics_bug_count(bug)
count = where(bug:"#{bug}").count
total = Rating.find_by_sql("SELECT bug FROM ratings WHERE bug <> ''").count
percentage = (count.to_f/total.to_f * 100)
return count, percentage
end
我的观点:
<table class="table table-bordered">
<tr>
<th><h3>Bug</h3></th>
<th><h3>Total Occurrences</h3></th>
<th><h3>Frequency</h3></th>
</tr>
<tr>
<%@unique_bug.each do |rating| %>
<% count, percentage = Rating.metrics_bug_count(rating.bug)%>
<td><p class="text-center"><h4><%= rating.bug.capitalize %></h4></p></td>
<td><p class="text-center"><h4><%= count %></h4></p></td>
<td><p class="text-center"><h4><%= number_to_percentage(percentage, :precision => 2)%></h4></p></td>
</tr>
<%end%>
</table>
如何使每一行(Bug、Total、Frequency)都可以排序?我在可排序表上观看Railscast剧集,但是那里的排序是在控制器中完成的。如何使用在我的模型中执行的更复杂的查找对我的表进行排序。