Rails Cheat Sheet: Scopes

Rails 3 allows you to define queries with named scopes, examples below.

scope :all_lazy, select('*')

scope :find_lazy, lambda{|id| where(primary_key => id)}

scope :all_ordered, all_lazy.order("<column_name> ASC")

Leave a Reply

Your email address will not be published. Required fields are marked *