嗨,我正在使用下面的代码
Companion.where(companion_type: 1).joins(:tasks).where(tasks: {status: 3}).group_by_month(:created_at).size
我有一个像这样的数据
Jan, 2017 => 89
.
.
.
Aug, 2021 => 300
但我需要2019年1月以上的数据有什么好的方法可以解出来吗?
如果您只对2019年1月感兴趣,那么您不需要拉出所有Companion表,然后按月对它们进行分组。相反,只选择感兴趣月份的那些…
Companion.
where(companion_type: 1).
joins(:tasks).
where(tasks: {status: 3}).
where("MONTH(tasks.created_at) = 1 AND DAY(tasks.created_at) = 19").
count
我假设你想要过滤的是任务的created_at
,而不是同伴的created_at
,对吗?
此外,最好使用count
而不是size
,因为它直接插入到sql中,而不是实例化从2019年1月开始的所有对象,然后对结果应用。size。