How can I select ALL student students from my database with active status only in the month of May? I couldn't find it in the active record documentation. I only managed to query the students who were created in May, and not ALL students actived....
Student.where(status:'active', created_at: ('2021-05-01'..'2021-05-31')).count
actually, my idea would go something like this:
Student.where(status:'active', datetime: (('2021-05-01'..'2021-05-31')).count
CodePudding user response:
You need to parse the Date like so:
Student.where(status:'active', created_at: (Date.parse('2021-05-01')..Date.parse('2021-05-31'))).count
You could also let Rails set the beginning and end of the month:
Student.where(status:'active', created_at: (Time.new(2021, 5).beginning_of_month..Time.new(2021, 5).end_of_month))).count
If status ist an enum you can also query like this:
Student.active.where(created_at: (Time.new(2021, 5).beginning_of_month..Time.new(2021, 5).end_of_month))).count