jrh*_*cks 60 activerecord ruby-on-rails ransack
我正在使用rails ransack(https://github.com/ernie/ransack)来允许用户过滤和排序某些记录.我使用传统方法获取过滤和排序的记录.
@invoices = Invoice.search(params[:q]).result
Run Code Online (Sandbox Code Playgroud)
现在我想得到一些摘要信息,所以我有
@invoices = Invoice.search(params[:q]).result
@summary = @invoices.select("sum(balance) as balance_total").first
Run Code Online (Sandbox Code Playgroud)
除非用户指定要排序的字段.我收到SQL错误:
Column "project_name" is invalid in the ORDER BY clause because
it is not contained in either an aggregate function or the GROUP BY clause
Run Code Online (Sandbox Code Playgroud)
我可以从范围中删除排序吗?怎么样?
谢谢
Mor*_*ori 147
您可以使用空字符串调用重新排序方法.例如:
> Article.order('headline asc').to_sql
=> "SELECT `articles`.* FROM `articles` ORDER BY headline asc"
> Article.order('headline asc').reorder('').to_sql
=> "SELECT `articles`.* FROM `articles` "
Run Code Online (Sandbox Code Playgroud)