我的排序有问题。
在对id
和mac
列进行排序时,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
感谢您的帮助。