Read more

Find records created within a timespan

Arne Hartherz
May 31, 2011Software engineer at makandra GmbH

Put the attached file into config/initializers/ to be able to say created_within on any ActiveRecord or its scope chain. This will return a scope of all records created between two given timestamps.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

Use it like that:
Booking.created_within(1.year.ago, Date.yesterday).count
User.active.created_within(1.hour.ago, DateTime.now)

It explicitly looks at the database table name so you are usually fine when joining other tables.

Posted by Arne Hartherz to makandra dev (2011-05-31 17:08)