ASCIIcasts - “Episode 215 - Advanced Queries in Rails 3”:

215: Advanced Queries in Rails 3 (view original Railscast) Other translations: Other formats: This episode will cover advanced queries in Rails 3. In episode 202 [watch , read ] we covered the additions to ActiveRecord queries in Rails 3; here we’ll carry on from there and show you some more advanced tips. Using Class Methods Instead of Scopes The application that we’ll be using in this application has two models: Product and Category with a product belonging to a category. The product model has two named scopes: discontinued , which returns all of the products for which discontinued is true and price, which returns the products that are cheaper than a given price argument. /app/models/product.rb class Product < ActiveRecord::Base belongs_to :category scope :discontinued, where(:discontinued => true) scope :cheaper_than, lambda { |price| where("price < ?", price) } end In ...