Skip to content

Instantly share code, notes, and snippets.

@valakirka
Forked from rails/gist:264500
Created December 29, 2009 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valakirka/265225 to your computer and use it in GitHub Desktop.
Save valakirka/265225 to your computer and use it in GitHub Desktop.
Rails 3
products = Product.where("price = 100").limit(5) # No query executed yet
products = products.order("created_at DESC") # Adding to the query, still no execution
products.each { |product| puts product.price } # That's when the SQL query is actually fired
class Product < ActiveRecord::Base
named_scope :pricey, where("price > 100")
named_scope :latest, order("created_at DESC").limit(10)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment