In a production app, the following code works in dev and (initially) in production:
@employee.ident_expiration_date && @employee.ident_expiration_date < 3.months.from_now
The app is used daily by one or more users. At random intervals, at least a week at most 3 months, this error starts to occur: ActionView::Template::Error (comparison of Date with ActiveSupport::TimeWithZone)
In the console:
>> date = e.ident_expiration_date
=> Sat, 12 Jun 2021
>> date.class
=> Date
>> twz = 3.months.from_now
=> Sat, 04 Nov 2017 10:11:54 CDT -05:00
>> twz.class
=> ActiveSupport::TimeWithZone
>> date < twz
=> false
I thought maybe I was requiring something that might cause that issue... but the only require statement in the entire /app directory is require 'valuable'
, a gem I wrote. I'm sure that isn't causing the issue.
The process has been alive since I last restarted the app (which caused the issue to disappear.)
product+ 3053 0.0 16.7 1981264 342088 ? Sl Jun16 50:32 puma 2.12.3 (tcp://0.0.0.0:9444) [20170323153916]
So my conclusion at this point is that one of those classes is being polluted by some code that doesn't run very often. I'm have run through the recent history of the app to force this to happen. But I haven't succeeded.
I'm open to any ideas about how this might be happening.
Based on the second reference below, my long-shot answer is to change TimeWithZone to a Date using @employee.ident_expiration_date && @employee.ident_expiration_date < 3.months.from_now.to_date
... but if someone could explain what's happening that would be fantastic.
Versions
Versions:
- rbx 2.5.2 which is ruby 2.1.0
- rails 4.2.3
- Linux version 4.9.15-x86_64-linode81 (maker@build) (gcc version 4.7.2 (Debian 4.7.2-5) )
References
Don't use Date == TimeWithZone, which makes sense.
Date.today > Time.now fails in some situations -- interestingly, I can reproduce their error in irb but not in the Rails console. I assume Rails is doing something to fix this? Could be related but since I can't reproduce it in the console, I'm not sure what to do about it.
Aucun commentaire:
Enregistrer un commentaire