2

I want to get all the records from the database which are create_at today. But when I do

Model.where('DATE(created_at) = ?',Date.today)

it returns only the last record that is created today. I have 3 records with created_at

2015-03-03 11:38:31
2015-03-03 11:59:00
2015-03-03 11:33:04

. But it returns only the record with '2015-03-03 11:38:31' this created_at date.

Also referred How to delete all records created today?, Getting all rows created today. I want to know why is this happening? Also tried

 Model.where("DATE(created_at) = ?",Date.today)

and

 Tracking::TrackingLogin.where("created_at >= ?", Time.zone.now.beginning_of_day)

Please someone explain what is happening in my code?

Community
  • 1
  • 1
Suganya
  • 701
  • 1
  • 9
  • 27

3 Answers3

12

Try the following:

Model.where("created_at >= ?", Time.zone.now.beginning_of_day)
kenorb
  • 155,785
  • 88
  • 678
  • 743
user3118220
  • 1,438
  • 12
  • 16
0

Model.where("created_at >= ?", Time.zone.now.beginning_of_day)

Model = your model name

(i.e,. User.where("created_at >= ?", Time.zone.now.beginning_of_day))

-1
Model.where("created_at >= ?", Date.today)
yms9654
  • 1
  • 1
  • This also returns only the first record. Not all the records. – Suganya Mar 03 '15 at 12:21
  • this isn't right. Date.today comes without timezone information, sql plan will looks like => SELECT "modes".* FROM "modes" WHERE "modes"."is_deleted" = 'f' AND (created_at >= '2016-05-17') which is queried by UTC of today. – catsky May 17 '16 at 03:53