Shabini Rajadas Shabini Rajadas - 1 month ago 9
Ruby Question

Select records created between a time range irregardless of the date of creation

I want to select a set of records created between 2 am to 3 am, irregardless of the date it was created. Is there any way to achieve this?


With PostgreSQL:

Model.where("created_at::time BETWEEN '14:00:00' AND '15:00:00'")

Is there any possibility to check between 23:00:00 to 01:00:00.


Model.where("created_at::time >= '23:00:00' AND created_at::time < '01:00:00'")

P.S. Before dealing with time queries make sure you are aware of the Rails default timezone (UTC) and issues connected to users' timezone differences.