Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Common issues

ernie edited this page Apr 11, 2012 · 5 revisions

Scopes

Like any other scopes, scopes that use the Squeel DSL must be wrapped in lambdas if you want them to be lazy evaluated. For instance:

# DO NOT USE!
scope :recent, where{created_at > 1.week.ago}

Won't work -- for long. The value of 1.week.ago will be interpreted when the model is loaded, and never again. Instead, use a lambda:

scope :recent, lambda { where{created_at > 1.week.ago} }

or a class method:

def self.recent
  where{created_at > 1.week.ago}
end
Clone this wiki locally