Rails activerecord: query to write by date time?

I am trying to query the entries in the table TimeSlot field start_date, which is of type datetime . Here is what I have tried so far that failed:

 TimeSlot.where(:start_date => DateTime.new(2010, 9, 1)) TimeSlot.where(:start_date => DateTime.new(2010, 9, 1).to_s) TimeSlot.where(:start_date => "2010-09-08") TimeSlot.where(:start_date => "2010-09-08 00:00:00") 

I would appreciate any help.

+4
source share
3 answers

Your inquiries look good to me.

Are you sure you have the corresponding line in db?

To debug, look at the logs / development.log file.

Added:

The problem may be temporary. Your request uses the time zone of your server. Your data may be stored in a different time zone.

+1
source

I am sure that this is also a watchmaking. Everything in the database is automatically converted to UTC on rails. Queries 1 and 4 should work if there is no bias.

0
source

From rubyonrails.org

Client.where ("created_at> =: start_date AND created_at <=: end_date", {: start_date => params [: start_date] ,: end_date => params [: end_date]})

or

Client.where ("created_at IN (?)", (PARAMS [: start_date] .to_date) .. (PARAMS [: end_date] .to_date))

Here is what I have for the request, all the time TimeSlot starts with "2010-09-08" if your start_date is a date field.

 TimeSlot.where("start_date >= ? AND start_date <= ?", "2010-09-08", "2010-09-08") 

If start_date is a datetime field.

 TimeSlot.where("start_date >= ? AND start_date <= ?", "2010-09-08", "2010-09-09") 

Since the date-time starts at 00:00:00

0
source

All Articles