Blog

Sunday, December 6, 2009

Combining Named Scopes

This is a useful way to combine named scopes:
# add to lib/named_scopes.rb for example
class ActiveRecord::Base
  def self.named_scopes(name, &block)
    metaclass.send(:define_method, name) {|*args| block.call(*args) }
  end
end 
Usage:
class Product
  named_scope :live, :conditions => {:active => true}
  named_scope :created_recently, :conditions => [created_at > ?", Time.now - 10.days]
  named_scopes(:recently_released) { live.created_recently  }   
end
Source: http://august.lilleaas.net/combining_named_scopes (no longer available)

Please note this blog is no longer maintained. Please visit CivilCode Inc - Custom Software Development.