Ruby on Rails

created_at Time Scopes for Ruby on Rails



Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /usr/www/phpsites/public/yayprogramming/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

I’ve compiled a simple list of time scopes for Ruby on Rails. With all models, they should have a ‘created_at’ column that will fit in with the scopes below.

1
2
3
4
5
6
7
8
9
10
11
12
# Time Scopes
scope :today, -> { where(:created_at => Time.now.beginning_of_day..Time.now.end_of_day) }
scope :yesterday, -> { where(:created_at => 1.days.ago.beginning_of_day..1.days.ago.end_of_day) }
 
scope :this_week, -> { where(:created_at => Time.now.beginning_of_week..Time.now.end_of_week) }
scope :this_month, -> { where(:created_at => Time.now.beginning_of_month..Time.now.end_of_month) }
scope :this_year, -> { where(:created_at => Time.now.beginning_of_year..Time.now.end_of_year) }
 
scope :last_hour, -> { where(:created_at => 1.hour.ago..Time.now) }
scope :last_week, -> { where(:created_at => 1.weeks.ago.beginning_of_week..1.weeks.ago.end_of_week) }
scope :last_month, -> { where(:created_at => 1.months.ago.beginning_of_month..1.months.ago.end_of_month) }
scope :last_year, -> { where(:created_at => 1.years.ago.beginning_of_year..1.years.ago.end_of_year) }

View Comments
There are currently no comments.