Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Putting Date and 1.month.ago together?

How would I put Date and 1.month.ago together when I have a date attribute called :purchase_date and want to put it inside a class method?

def self.last_month # Show only products of last month.
  where(:purchase_date => Date.today.1.month.ago.end_of_month..Date.1.month.ago.beginning_of_month)
end

console gives a syntax error and taking it away Date.today gives me blank results compared to my other method:

def self.this_month # Show only products of this month.
   where(:purchase_date => Date.today.beginning_of_month..Date.today.end_of_month)
end
like image 501
LearningRoR Avatar asked Dec 15 '11 13:12

LearningRoR


2 Answers

Just 1.month.ago is enough, you don't need to prepend Date.today to 1.month.ago because 1.month.ago starts from today

like image 79
Marek Příhoda Avatar answered Oct 22 '22 02:10

Marek Příhoda


You have mistake in your Date syntax, you might want to use something like this:

def self.last_month # Show only products of last month.
  where(:purchase_date => 1.month.ago.beginning_of_month..1.month.ago.end_of_month)
end

def self.this_month # Show only products of this month.
   where(:purchase_date => Date.today.beginning_of_month..Date.today.end_of_month)
end
like image 23
Mikhail Nikalyukin Avatar answered Oct 22 '22 03:10

Mikhail Nikalyukin