Recently I needed my APIs to filter index responses with records updated since a particular date and time. This requirement was not just for a single API, but for almost all my index queries. To add to the requirements, the new
updated_since param is optional. Rather than adding a scope to each model to handle this optional param, I made use of the new
ActiveSupport::Concern class and included it in the relevant models. By moving the
updated_since scope to a module, I not only prevented code duplication, but was able to add logging without bloating my models.
UpdatedSince module can be seen below, or take a look at the Gist
if parsed_time = convert_time(time)
where("updated_at >= ?", parsed_time)