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
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With