default_scope and unscoped

classPhoto<ActiveRecord::Base...
  default_scope where(:status=>'complete')
  scope :batch, unscoped.where(:status=>'batch')
  scope :queue, unscoped.where(:status=>'queue')
scope :ongoing, -> { where(end_at: nil) }
scope :where_ceid_and_key, ->(ceid, key){where(["`ceid` = ? AND `key` = ?", ceid, key])}
...end
定义 或 去掉默认排序
class Post < ActiveRecord::Base
  def self.default_scope
    where :published => true
  end
end

Post.all          # Fires "SELECT * FROM posts WHERE published = true"
Post.unscoped.all # Fires "SELECT * FROM posts"
原文地址:https://www.cnblogs.com/qinyan20/p/3643214.html