我在 rails 中没有使用 Activerecord 获得前几个月的 31 的费用

I dont get the expenses of the 31 of the previous months with Activerecord in rails

当我显示PUQ的总支出没有问题

Expend.where(cc: "PUQ").sum(:total)

但是当我想用这段代码按月单独显示时,我没有得到 31 的费用,只显示当月的 1 到 30。现在我没有收到 31-08-2020 的费用

Expend.where(cc: "PUQ").where(:created_at => ((Date.today.beginning_of_month)- 1.month)..((Date.today.end_of_month) - 1.month)).sum(:total)

提前致谢

您可以使用这些方便的 Rails 方法

range = Time.current.advance(months: -1).all_month
# => Sat, 01 Aug 2020 00:00:00 UTC +00:00..Mon, 31 Aug 2020 23:59:59 UTC +00:00


Expend.where(created_at: range)

更多关于all_month 大约 advance