Ransack不会对计数列进行排序?

时间:2014-03-10 10:39:25

标签: ruby-on-rails ruby ruby-on-rails-4 ransack

我的排序有问题。

在对idmac列进行排序时,Ransack可以正常工作,但它不适用于frequency列。在日志中,当我点击frequency时没有出现任何内容。

型号:

scope :with_frequency, -> {
select('devices.*,tr.times AS frequency').
joins('join (SELECT device_id,count(device_id) AS times FROM timed_requests tr GROUP BY device_id) AS tr ON tr.device_id = devices.id')}

控制器:

def index
@search = Device.with_frequency.search(params[:q])
@search.sorts = 'frequency desc' if @search.sorts.empty?
@devices = @search.result.paginate(page: params[:page], per_page: 20)
end

查看:

%th= sort_link @search, :id, "ID"
%th= sort_link @search, :mac, "MAC-Addresse"
%th= sort_link @search, :frequency, "Frequency"

记录(首先点击频率而不是ID):

Parameters: {"page"=>"1", "q"=>{"s"=>"frequency desc"}}
Device Load (39.8ms)  SELECT devices.*,tr.times AS frequency FROM "devices" join (SELECT     device_id,count(device_id) AS times FROM timed_requests tr GROUP BY device_id) AS tr ON        tr.device_id = devices.id LIMIT 20 OFFSET 0

Parameters: {"page"=>"1", "q"=>{"s"=>"id asc"}}
Device Load (61.3ms)  SELECT devices.*,tr.times AS frequency FROM "devices" join (SELECT device_id,count(device_id) AS times FROM timed_requests tr GROUP BY device_id) AS tr ON tr.device_id = devices.id ORDER BY "devices"."id" ASC LIMIT 20 OFFSET 0

感谢您的帮助。

0 个答案:

没有答案