New to Rails 3? Check out the Ruby on Rails 3 Tutorial book and screencast.

A book and screencast series showing you how to develop and deploy industrial-strength Rails apps in a direct, step by step way. The screencast series includes 12 lessons over more than 15 hours! Get the best "over the shoulder" experience of following what a top Rails 3 developer does when building an app today. Click here to learn more.

New Edge Rails Feature: Default Scoping

In Miscellaneous, Tips

In his "What's New in Edge Rails" series, Ryan Daigle has just written about "default scoping." You're probably familiar with regular named scopes on Rails models, but now there's a default_scope method that defines a scope that exists by default on find methods.

Example:

class Article < ActiveRecord::Base
  default_scope :order => 'created_at DESC'
end

The default scope also carries across, by default, to any regular named scopes that you might have, so if you want to override the default scope, you have to force it.

Do note, however, that this feature is currently only in edge rails.

Post to Twitter Tweet This Post

Vaguely Related Posts (Usually)

Leave a Reply