Q:

Rails 4 save wrong utc in the mysql database

I need to use the parameter in my aplication.rb
config.active_record.default_timezone = :utc 
My project has some GEM 'S ( groupdate , SearchJoy ) that require it. 
I am facing problem where created_at and updated_at columns are saved with the wrong time (time + 2 )
Example extract from Rails Console:
[2] pry(main)> >> System.create(name: 'TEST')
   (0.5ms)  BEGIN
  SQL (1.0ms)  INSERT INTO `systems` (`name`, `created_at`, `updated_at`) VALUES ('TEST', '2015-12-09 20:44:14', '2015-12-09 20:44:14')
   (3.2ms)  COMMIT
=> #<System:0xbc487a6c
 id: 10,
 name: "TEST",
 created_at: Wed, 09 Dec 2015 18:44:14 BRST -02:00,
 updated_at: Wed, 09 Dec 2015 18:44:14 BRST -02:00>
[3] pry(main)> 

My project uses Rails 4.2.2 and Mysql
aplication.rb :
   # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.
    config.active_record.default_timezone = :utc

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'
    config.time_zone = 'Brasilia'

Rails masters any ideas?

A:

It looks like it is working correctly.
Your rails application is running in the timezone Brasilia and saving into the database using :utc, so the value in the database is the correct UTC value, when you pull the records out of the database it is automatically converting to the application timezone.
System.create(name: 'TEST') # this is in Brasilia, the data saved to the db should be offset from Brasilia to UTC
Time.use_zone { puts System.last.updated_at } # view the time in UTC
puts System.last.updated_at # view the time in Brasilia

I recommend you change your config.time_zone = 'UTC' and then adjust the time_zone per user as needed (per request)
I wrote a blog post on rails and timezones you might find helpful: http://jessehouse.com/blog/2013/11/15/working-with-timezones-and-ruby-on-rails/

