Find records created within a timespan

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.

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.

Arne Hartherz