Skip to content

Instantly share code, notes, and snippets.

@wellington1993
Created May 16, 2019 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wellington1993/01b84c5d5abf1f3ab50d0eb98282ab7e to your computer and use it in GitHub Desktop.
Save wellington1993/01b84c5d5abf1f3ab50d0eb98282ab7e to your computer and use it in GitHub Desktop.
Default Scope Rails
default_scope
This works for Rails 4+:
class Book < ActiveRecord::Base
default_scope { order(created_at: :desc) }
end
For Rails 2.3, 3, you need this instead:
default_scope order('created_at DESC')
For Rails 2.x:
default_scope :order => 'created_at DESC'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment