Excerpt

## default_scope
This works for Rails 4+:
```plain text
class Book < ActiveRecord::Base
default_scope { order(created_at: :desc) }
end
```
For Rails 2.3, 3, you need this instead:
```plain text
default_scope order('created_at DESC')
```
For Rails 2.x:
```plain text
default_scope :order => 'created_at DESC'
```
Where created_at is the field you want the default sorting to be done on.
Note: ASC is the code to use for Ascending and DESC is for descending (desc, NOT dsc !).
## scope
Once you're used to that you can also use scope:
```plain text
class Book < ActiveRecord::Base
scope :confirmed, :conditions => { :confirmed => true }
scope :published, :conditions => { :published => true }
end
```
For Rails 2 you need named_scope.
:published scope gives you Book.published instead of Book.find(:published => true).
Since Rails 3 you can 'chain' those methods together by concatenating them with periods between them, so with the above scopes you can now use Book.published.confirmed.
With this method, the query is not actually executed until actual results are needed (lazy evaluation), so 7 scopes could be chained together but only resulting in 1 actual database query, to avoid performance problems from executing 7 separate queries.
You can use a passed in parameter such as a date or a user_id (something that will change at run-time and so will need that 'lazy evaluation', with a lambda, like this: