【问题标题】:Ransack sort_link for SQL COUNT(), SUM and AVERAGE搜索 SQL COUNT()、SUM 和 AVERAGE 的 sort_link
【发布时间】:2016-03-11 17:01:33
【问题描述】:

我想为以下查询添加排序链接:

@q = Order.group(:suburb).select("suburb, COUNT(*) as count, SUM(total) as total, AVG(total) as average").ransack(params[:q])

但是当我使用时:

<%= sort_link(@q, :average) %>

URL 被正确填充,但没有排序! 当我检查@q.sorts 时,这些列是正确的。

【问题讨论】:

    标签: ruby-on-rails ransack


    【解决方案1】:

    数据库中没有平均列,所以它不会那样工作。你可以尝试用 ransacker 破解它:

    ransacker :average do
      Arel.sql('average')
    end
    

    所以当找到平均值时将使用这个平均值。

    【讨论】:

    • 我有 this 一些与赏金相关的问题。你想试一试吗?
    猜你喜欢
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多