Remove order from ActiveRecord scope

时光怂恿深爱的人放手 提交于 2019-11-30 02:36:43

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` "

You can also use the unscoped class method in Rails 3:

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

In Rails 2 it was called with_exclusive_scope.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!