arrow_backward Back to blog

Codebite: Generic “New Today” for Rails Records

Often times in an application you might want to know how many of a given model were created today. Rather than writing a custom method for each model, let’s keep it DRY and extend ActiveRecord:

This will return the number of records created in the last 24 hours by calling the model with new_today (e.×. User.new_today). It will return nil if the model does not respond to the created_at Rails timestamp attribute.

arrow_backBack

New Project Request