K
K
Karina2017-06-15 01:56:54
Ruby on Rails
Karina, 2017-06-15 01:56:54

How to save time in a model with type Time given the time zone?

There is a field in the model that is of type datetime. The user selects the time. And always either +3 hours or -3 hours. I tried to set the time in before_create to this field in_time_zone('Moscow'), in application.rb I tried to write config.active_record.default_timezone = 'Moscow' or 'UTC'; config.time_zone = 'Moscow' or 'UTC' . And in general, I erased everything in application.rb related to time. But I can't set the right time.
Thanks to all.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Shaks, 2017-06-15
@iKapex

iKapex
Take off your shorts and run :)
Yes. really. I indulged in the console, I saw this behavior.
This happens because:
This behavior is fixed quite simply.

# config/application.rb
..........
config.active_record.time_zone_aware_attributes = false

Result: h_1497532143_2268431_bf18b998c8.png
Read more here api.rubyonrails.org/classes/ActiveRecord/Timestamp.html
PS but I would advise you to leave this behavior, because it is right. And set in settings
#config/application.rb
config.active_record.default_timezone = :local

This way AR will convert back to the local timezone. And since you didn’t have it set, then it left UTC

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question