2016-10-23 68 views
0

我使用will paginate和ransack構建應用程序。Rails 5在分組時顯示結果數量

我的控制器:

def index 

    @q = Stockdiary.all.ransack(params[:q]) 
    @stockdiaries = @q.result.joins(:product).group(:PRODUCT).select("SUM(UNITS) AS UNITS, SUM(PRICE) AS PRICE").paginate(:page => params[:page], :per_page => 30) 

    end 

然後在我看來,我有這樣的展示中的條目數:

<div> <%= pluralize(@stockdiaries.size, 'stockdiary') %> found</div> 

但正如我在申請組,它顯示

{"1"=>6, "103"=>2, "104"=>1, "106"=>2, "115"=>2, "116"=>1, "118"=>1, "122"=>1, "124"=>2, "13"=>6, "130"=>1, "133"=>3, "137"=>2, "138"=>2, "14"=>2, "140"=>1, "142"=>1, "144"=>1, "145"=>1, "146"=>2, "148"=>3, "149"=>1, "151"=>3, "152"=>3, "154"=>4, "16"=>4, "160"=>1, "161"=>1, "163"=>2, "166"=>1, "170"=>2, "174"=>1, "18"=>2, "19"=>1, "2"=>1, "25"=>3, "27"=>2, "29"=>1, "31"=>1, "33"=>1, "34"=>1, "43"=>2, "49"=>2, "56"=>1, "70"=>6, "73"=>4, "83"=>1, "9"=>2, "9e249a40-82d3-499f-a2b8-9f8613ecfe81"=>2, "bcea85d5-2596-4e41-8245-e520bc2b07a8"=>2} stockdiaries found 

它顯示每個產品的條目數量(因爲它按產品分組)。很顯然,我只需要展示所有參賽作品,而不是每個產品。

如何解決這個問題?

回答

1

您可以使用sum

<div><%= pluralize(@stockdiaries.size.values.sum, 'stockdiary') %> found</div> 
+0

這工作。謝謝! – Catmal