How to find the record created in the previous month?

I have a table where new entries are added daily. How can I find records created in the previous month?

+6
ruby ruby-on-rails
source share
10 answers

Setting up a named scope:

named_scope :in_last_month, :conditions => [ "records.created_at > ?", 1.month.ago ] 

To call it (in your controller):

 Record.in_last_month 
+11
source share

named_scope is a pretty elegant way, I think, but if you take this route, you will want to use it using the lambda method so that time does not fall into the area when the application is initially loaded.

For example, this:

 named_scope :last_month, :conditions => ['created_at > ? AND created_at < ?', Date.today.last_month.beginning_of_month, Date.today.beginning_of_month] 

will work properly in the first month when your application will work, but incorrectly in the next month if the application does not restart.

But this:

 named_scope :last_month, lambda { {:conditions => ['created_at > ? AND created_at < ?', Date.today.last_month.beginning_of_month, Date.today.beginning_of_month]}} 

will work every time, because the lambda method is run on every call, overestimating Date.today s.

+10
source share

Assuming your records have a timestamp, you can simply do something like this:

 Thing.find(:all, :conditions => ["created_at > ?", Time.now - 1.month]) 

If they are not tied to a timestamp, you should start storing information, as this is what you will want to view later.

+2
source share

Thanks to everyone, I ended up with this:

 find(:all, :conditions => ['created_at > ? AND created_at < ?', Date.today.last_month.beginning_of_month, Date.today.beginning_of_month]) 
+1
source share

In one of my projects, I used this method:

 Thing.where('created_at BETWEEN ? AND ? ', DateTime.now.beginning_of_month - 1.month, DateTime.now.beginning_of_month) 

in Rails 3 using last_month throws an error: Date.today.last_month.beginning_of_month

NoMethodError: undefined method `last_month '

+1
source share

Accepted answer and improved lambda do not work in Rails 4.

Update for Rails 4:

 scope :last_month, -> { where( 'created_at > ? AND created_at < ?', Date.today.last_month.beginning_of_month, Date.today.beginning_of_month )} 
+1
source share

Do you have β€œregular” fields on your desk? See the RoR wiki for a list of them. This way you can express special requests to find the answer.

0
source share
 Thing.find(:all, :conditions => ["created_at > ?", 1.month.ago.at_beginning_of_month]) 
0
source share

I don't have enough reputation to vote, but please pay attention to the comment that is mentioned in using lambdas in named_scopes. There the Railscasts episode on this subject should also be useful:

http://railscasts.com/episodes/108

0
source share

it is a good candidate for SQL BETWEEN syntax

 named_scope :last_month, :conditions => ['created_at BETWEEN ? AND ?', Date.today.last_month.beginning_of_month, Date.today.beginning_of_month]) 
0
source share

All Articles