Suganya
Suganya

Reputation: 741

Get all records created today in Rails

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?

Upvotes: 3

Views: 5649

Answers (3)

bansalsb57
bansalsb57

Reputation: 1

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))

Upvotes: 0

user3118220
user3118220

Reputation: 1468

Try the following:

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

Upvotes: 13

yms9654
yms9654

Reputation: 1

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

Upvotes: -1

Related Questions