How do I Create a Scope for Last Month?

I have a Rails 3.2 app with an Article model that has a field for date. I want to create a scope that will retrieve all records from last month. The problem I’m having is that my current scope is not including the first day of the month.

article.rb

scope :last_month, lambda { { conditions: { date: last_month_range } } }

private

def self.last_month_range

1.month.ago.beginning_of_month…1.month.ago.end_of_month

end

When I run this it does this:

SELECT * FROM ‘articles’ WHERE ('articles`.‘date’ BETWEEN ‘2013-07-01 07:00’ AND ‘2013-08-01 06:59:59’)

When I look at the actual results it only starts with the articles with a date of 07-02-2013.

However, if I change the code to:

def self.last_month_range

(1.month.ago.beginning_of_month - 1.day)…1.month.ago.end_of_month

end

It does this:

SELECT * FROM ‘articles’ WHERE ('articles`.‘date’ BETWEEN ‘2013-06-30 07:00’ AND ‘2013-08-01 06:59:59’)

In that case it pulls in articles with dates of 6/31/2013.

Can someone please recommend a fix? Thank you!

This mailing list for the discussion of development of core Rails features. For support please consider using Stackoverflow, Quora, #RubyOnRails on IRC or the rubyonrails-talk mailing list.

I sent you a reply off list.

-Al