如何查询按日期过滤的活动记录?



如何选择ALL学生学生从我的数据库与活动状态仅在五月?我在现行记录文件中找不到。我只查询了五月份创建的学生,没有查询ALL学生中共…

Student.where(status:'active', created_at: ('2021-05-01'..'2021-05-31')).count

实际上,我的想法是这样的:

Student.where(status:'active', datetime: (('2021-05-01'..'2021-05-31')).count

您需要像这样解析Date:

Student.where(status:'active', created_at: (Date.parse('2021-05-01')..Date.parse('2021-05-31'))).count

你也可以让Rails设置月的开始和结束:

Student.where(status:'active', created_at: (Time.new(2021, 5).beginning_of_month..Time.new(2021, 5).end_of_month))).count

如果状态列表是一个枚举,你也可以像这样查询:

Student.active.where(created_at: (Time.new(2021, 5).beginning_of_month..Time.new(2021, 5).end_of_month))).count