Find records created within a timespan

Updated . Posted . Visible to the public.

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
Last edit
Attachments
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2011-05-31 15:08)