Wednesday, May 03, 2023

Find all users created yesterday

 To find all User records with a created_at timestamp of yesterday using ActiveRecord in Rails:


yesterday = Date.yesterday.beginning_of_day..Date.yesterday.end_of_day

users_created_yesterday = User.where(created_at: yesterday)


This code first creates a range representing yesterday's date from the beginning of the day to the end of the day. Then, it queries the User model using the where method to filter the records based on the created_at timestamp within the specified range. The result is an ActiveRecord relation containing all User records created yesterday.