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?


class Model
  scope :this_month, -> { where(created_at: }

you can call it like this:


I prefer SQL-less:

Model.where(:created_at =>

or as a scope:

class Model < ActiveRecord::Base
  scope :this_month, -> { where(:created_at => }

Depends on if your looking for all records of all models or on a specific model etc...

For a single model you could do:

User.where('created_at >= ? and created_at <= ?',,

The answers providing scope examples are wrong. Rails uses eager-evaluation on scopes and so will always be the start of the month when the scope was initially evaluated. The correct way to use dates in a scope is to pass a lambda to the scope.

class Model < ActiveRecord::Base
  scope :this_month, -> { where(created_at: }

In Rails 4 scopes must use a callable object such as Lambda or Proc

Need Your Help

Redirecting bash output to a path stored in a variable

linux bash shell ubuntu

I'm trying to redirect my output to a file which has its path stored in a variable but I cant get it to work.

How to show a Progress Bar inside an AlertDialog Builder along with other elements without an XML?

java android android-layout progress-bar alertdialog

I am trying to create an AlertDialog for a Bluetooth transfer after the transfer notification is touched in an android phone.