Rails 4查找数据,其中日期是该月的最后一天

时间:2015-03-09 22:18:23

标签: ruby-on-rails date ruby-on-rails-4 where

使用Rails 4,我有一组数据,我只想要数据点,其中日期是月份的最后一天,任何月份,但只有日期是该月的最后一天。

下面我将@data变量设置为我的所有统计数据,将其限制为日期在去年的统计数据。我还想将数据限制为仅包含日期为该月最后一天的统计信息。

修改 - 请注意,开始日期是变量,可以通过参数

进行更改
startdate = Date.1.year.ago
@data = Statistic.where(date: startdate..Date.today).all

是否有一种简单的方法可以添加另一个.where子句并为此使用end_of_month?

1 个答案:

答案 0 :(得分:0)

将此scope写在Statistics模型中:

scope :last_date_stats, ->(dates) { where(date: dates) }

dates中填充StatisticsController数组,然后使用范围查询获取特定数据:

dates = (0..11).to_a.map { |n| n.months.ago.end_of_month }    
@last_date_statistics = Statistics.last_date_stats(dates)
相关问题