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 professionals since 2007

Our laser focus on a single technology has made us a leader in this space. Need help?

  • We build a solid first version of your product
  • We train your development team
  • We rescue your project in trouble
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)