Entering date and 1.month.ago together?

How do 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 removes it Date.today gives me empty 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 
+4
source share
2 answers

You have an error in the Date syntax, you can 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 
+7
source

Just 1.month.ago enough, you do not need to add Date.today to 1.month.ago , because 1.month.ago starts today

+8
source

Source: https://habr.com/ru/post/1386523/


All Articles