我正在尝试查询过去几个月发送的邮件数量,我使用类似的内容查找过去6个月的名称
gon.now_month_label = (Date.today).strftime("%B")
gon.now_month_data =
gon.one_month_ago_label = (Date.today - 1.month).strftime("%B")
gon.one_month_ago_data =
gon.two_months_ago_label = (Date.today - 2.month).strftime("%B")
gon.two_months_ago_data =
gon.three_months_ago_label = (Date.today - 3.month).strftime("%B")
gon.three_months_ago_data =
gon.four_months_ago_label = (Date.today - 4.month).strftime("%B")
gon.four_months_ago_data =
gon.five_months_ago_label = (Date.today - 5.month).strftime("%B")
gon.five_months_ago_data =
但也需要相应的数据。
我试过这样的事情
gon.now_month_data = SentMessage.where(created_at.month: (Date.today).strftime("%B") ).count
但它失败了。除此之外,它可以搜索其他年份而不是一个月前。 知道我如何构建查询以获取信息。
我们需要有11月份的消息:2034年消息和10月份:5330
答案 0 :(得分:1)
gon.three_months_ago_data = SentMessage.where('created_at > ? and created_at < ?', Date.today.beginning_of_month - 3.months, Date.today.beginning_of_month - 2.months).count
答案 1 :(得分:0)
查看by star插件,它可以让您执行以下操作:
SentMessage.by_month("January") # of current year
SentMessage.by_month("January",:year => 2007)
SentMessage.by_month(1, :year => 2007)
SentMessage.between(time1, time2)