Getting all the records of a model created today in Rails
April 11, 2022
My favorite thing I've learned recently is something I learned when needing to remove a bunch of records that had just been created in a Rails app.
If you want to get all the records of model Post
created on the current day, you can do:
Post.where(created_at: Time.current.all_day)
An alternative is
Post.where(created_at: Time.zone.now.beginning_of_day..Time.zone.now.end_of_day)
but the Time.current.all_day
version is so simple!
Thanks to this StackOverflow post for the solution!