#108 named_scope

The named_scope method in Rails 2.1 makes performing finds on models very elegant and convenient. See how in this episode.
# models/product.rb
class Product < ActiveRecord::Base
  belongs_to :category
  named_scope :cheap, :conditions => { :price => 0..5 }
  named_scope :recent, lambda { |*args| {:conditions => ["released_at > ?", (args.first || 2.weeks.ago)]} }
  named_scope :visible, :include => :category, :conditions => { 'categories.hidden' => false }
end

你可能感兴趣的:(ActiveRecord,Rails)