Question: How do we safely compare a date to an unknown value within a Rails application?
Example: params[:date_field] > 2.years.ago
Doing a bit of digging, it looks like 2.years.ago
returns an instance of ActiveSupport::TimeWithZone
.
In ruby, whenever you do a comparison of any sort ruby will follow the rules outlined in the Comparable
module of stdlib.
ActiveSupport::TimeWithZone
will create a instance of Time
in a UTC time zone when doing comparisons with ActiveSupport::TimeWithZone#<=>
.