2013-04-09 46 views
0

我的控制器:排序我的Rails表

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> 

我怎樣才能使每一行(錯誤,道達爾,頻率)每個都可以排序?我在可排序的桌子上觀看Railscast插曲,但是排序在控制器中完成。如何將我的表格與我的模型中執行的更復雜的查找進行排序。

回答