Remove order from ActiveRecord scope

前端 未结 2 1249
忘掉有多难
忘掉有多难 2020-12-25 09:17

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 me

相关标签:
2条回答
  • 2020-12-25 10:02

    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` "
    
    0 讨论(0)
  • 2020-12-25 10:02

    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

    0 讨论(0)
提交回复
热议问题