Rails4 查找日期为该月最后一天的数据
Rails 4 find data where the date is the last day of the month
使用Rails 4,我有一组数据,我只想要日期在该月最后一天的数据点,任何一个月,但只想要日期是最后一天的数据点月的。
下面我设置了@data 变量,将我的所有统计数据限制为去年日期的统计数据。我还想将数据限制为仅包含日期为当月最后一天的统计信息。
编辑
- 请注意,开始日期是一个变量,可以通过参数更改
startdate = Date.1.year.ago
@data = Statistic.where(date: startdate..Date.today).all
有没有简单的方法来添加另一个 .where 子句并为此使用 end_of_month?
在你的Statistics
模型中写下scope
:
scope :last_date_stats, ->(dates) { where(date: dates) }
在您的 StatisticsController
中填充一个 dates
数组,然后使用范围查询获取特定数据:
dates = (0..11).to_a.map { |n| n.months.ago.end_of_month }
@last_date_statistics = Statistics.last_date_stats(dates)
使用Rails 4,我有一组数据,我只想要日期在该月最后一天的数据点,任何一个月,但只想要日期是最后一天的数据点月的。
下面我设置了@data 变量,将我的所有统计数据限制为去年日期的统计数据。我还想将数据限制为仅包含日期为当月最后一天的统计信息。
编辑 - 请注意,开始日期是一个变量,可以通过参数更改
startdate = Date.1.year.ago
@data = Statistic.where(date: startdate..Date.today).all
有没有简单的方法来添加另一个 .where 子句并为此使用 end_of_month?
在你的Statistics
模型中写下scope
:
scope :last_date_stats, ->(dates) { where(date: dates) }
在您的 StatisticsController
中填充一个 dates
数组,然后使用范围查询获取特定数据:
dates = (0..11).to_a.map { |n| n.months.ago.end_of_month }
@last_date_statistics = Statistics.last_date_stats(dates)