Technology Musings

July 02, 2008

Features / named_scope is really cool

JB

Rails 2.1.0 has, among other things, one very cool feature - named scoping.

class Whatever < ActiveRecord::Base
  named_scope :active, :conditions => { :active => true }
end
 
Whatever.active  #yields all active whatevers
Whatever.active.find(x) #only runs find() on active whatevers

You can even have parameters that get passed in through anonymous functions. It's a very powerful mechanism, and makes everything much more beautiful.  Learn more here.