-
-
Save amiel/3642768 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo < ActiveRecord::Base | |
extend TimeScopes | |
# ... | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
: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