attr_json icon indicating copy to clipboard operation
attr_json copied to clipboard

ActiveRecord time_zone_aware_attributes not operative in attr_json date/time types

Open jrochkind opened this issue 6 years ago • 0 comments

ActiveRecord.time_zone_aware_attributes is true by default, and what it does is turn all your AR date/time attributes into magical ActiveSupport::TimeWithZone objects that (basically) have the current Time.zone zone attached to them. The time might be stored in the db in UTC but it comes back as a TimeWithZone in Time.zone. (you don't even need to save for this to happen, it happens as soon as you set an attribute).

Our json_attribute :date and :datetime typed fields do not have this property. They remain plain old ruby stdlib Time or Date objects, they are not magically turned into ActiveSupport::TimeWithZone.

They are stored in the db always in UTC. They are correct, they don't get the time wrong. If you set a local time, it will be stored in the db in UTC, representing the same moment in time. And it will come back in UTC. You can convert it to whatever time zone you want for display purposes using ActiveSupport API -- but it won't automatically be that.

I'm not sure if this matters, it may be okay. On the other hand, it may lead to hard to deal with behavior when letting users edit times in a form (what zone are those times in exactly?).

We need more info on how important/useful it is before spending time on figuring out how to get our own date/time types to respect ActiveRecord::Base.time_zone_aware_attributes.

Feedback welcome.

There are some tests marked "pending" that would pass if our attr's were properly respecting/implementing ActiveRecord::Base.time_zone_aware_attributes == true behavior.

jrochkind avatar Apr 14 '18 05:04 jrochkind