Skip to content

Instantly share code, notes, and snippets.

@amiel
Forked from anonymous/gist:3642700
Created September 5, 2012 19:09
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 amiel/3642768 to your computer and use it in GitHub Desktop.
Save amiel/3642768 to your computer and use it in GitHub Desktop.
class Foo < ActiveRecord::Base
extend TimeScopes
# ...
end
module TimeScopes
def today(now = Time.current)
where('created_at BETWEEN ? AND ?', now.at_beginning_of_day, now.at_end_of_day)
end
def yesterday(now = Time.current)
today(now - 1.day)
end
# ...
end
{
:today => where(['created_at > ? and created_at < ?', Time.current.at_begi…
:yesterday => where(['created_at…
}.each {|name, time_period|
Foo.in_time_period.count # magic?
Bar.in_time_period.count # same magic!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment