从ActiveRecord范围中删除顺序

我正在使用rails ransack( https://github.com/ernie/ransack )来允许用户筛选和sorting一些logging。 我使用传统的方法得到过滤和sorting的logging。

@invoices = Invoice.search(params[:q]).result 

现在我想得到一些总结信息,所以我有

  @invoices = Invoice.search(params[:q]).result @summary = @invoices.select("sum(balance) as balance_total").first 

除了当用户指定一个字段进行sorting。 我得到了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 

我可以从范围中删除sorting吗? 怎么样?

谢谢

您可以使用空string调用重新sorting方法。 例如:

 [1] pry(main)> Article.order('headline asc').to_sql => "SELECT `articles`.* FROM `articles` ORDER BY headline asc" [2] pry(main)> Article.order('headline asc').reorder('').to_sql => "SELECT `articles`.* FROM `articles` " 

你也可以在Rails 3中使用unscoped类的方法:

 class Post < ActiveRecord::Base default_scope :published => true end posts = Post.all #=> SELECT * FROM posts WHERE published = true posts = Post.unscoped do Post.all #=> SELECT * FROM posts end 

在Rails 2中,它被称为with_exclusive_scope

https://github.com/rails/rails/commit/bd1666ad1de88598ed6f04ceffb8488a77be4385