I currently have a plain log with a matching value and a timestamp date. The matching value is not always present, so the second possible option is to match through the timestamp.
?timestamp=20120426140449
This query runs ok.
Subscription.find_by_created_at("20120426140449".to_date)
But since it’s the log is from a third-party API that post to my service, the timestamp do not necessarily match between the two sides.
My question is. How it is possible to try to match the exact date and if not try to look for the closest one in a given range of time?
First I would use
to_datetimeorto_timeinstead ofto_date, becausecreated_atis a datetime column and not only a date.Now
Subscription.for_timestamp_with_delta("20120426140449", 5)will return all subscriptions created within ±5 seconds of “Thu, 26 Apr 2012 14:04:49”