Register now and start sharing your code snippets.
-->

How to use named_scope in Rails

Ruby posted 5 months ago by christian

Simple example of how to use the named_scope feature:

   1  class Feed < ActiveRecord::Base
   2   
   3    named_scope :active, :conditions => "(active = 1)"
   4    named_scope :stale,  :conditions => ["last_updated > ?", 30.minutes.ago.to_s(:db)]

Usage:

   1  Feed.active # return the active feeds

Chaining is also possible:

   1  Feed.active.stale # return the feeds that need to be updated

Tagged named_scope, rails, activerecord, ruby