Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find records created this month in Rails 3.1?

Anyone know the best query to find records created THIS month in Rails 3.1?

like image 518
kidcapital Avatar asked Nov 19 '11 22:11

kidcapital


2 Answers

class Model
  scope :this_month, -> { where(created_at: Time.now.beginning_of_month..Time.now.end_of_month) }
end

you can call it like this:

Model.this_month
like image 128
Alex Peattie Avatar answered Sep 20 '22 14:09

Alex Peattie


I prefer SQL-less:

Model.where(:created_at => Time.now.beginning_of_month..Time.now.end_of_month)

or as a scope:

class Model < ActiveRecord::Base
  scope :this_month, -> { where(:created_at => Time.now.beginning_of_month..Time.now.end_of_month) }
end
like image 23
clem Avatar answered Sep 21 '22 14:09

clem