从ActiveRecord范围中删除订单 [英] Remove order from ActiveRecord scope
本文介绍了从ActiveRecord范围中删除订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Rails ransack( https://github.com/ernie/ransack )允许用户过滤和排序一些记录。我使用传统方法获得了经过过滤和排序的记录。
I'm using rails ransack ( https://github.com/ernie/ransack ) to allow the users to filter and sort some records. I get the filtered and sorted records using traditional methods.
@invoices = Invoice.search(params[:q]).result
现在我想获得一些摘要信息,所以我有
Now I would like to get some summary information so I have
@invoices = Invoice.search(params[:q]).result
@summary = @invoices.select("sum(balance) as balance_total").first
用户指定要排序的字段时除外。我收到SQL错误:
Except when the user specifies a field to sort. I get the SQL error:
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
我可以从范围中删除排序吗?
Can I remove the sort from the scope? How?
谢谢
推荐答案
您可以致电重新排序方法,其中包含空字符串。例如:
You can call the reorder method with an empty string. E.g.:
> 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` "
这篇关于从ActiveRecord范围中删除订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文