LightBe Corp LightBe Corp - 1 year ago 78
Ruby Question

Rails 4 - Want To Have Rails Use My Local Time Zone for created_at & updated_at

Is there actually a way to have Rails add/update rows with created_at and update_at using the current time zone as set on my server?

I have seen many Stack Overflow solutions where people stated how to display it on a Rails view with a selected time zone.

I have also found other Stack Overflow solutions stating that it should take the time zone from my server and update created_at and updated_at. This is not true at least for the Mac Mini Server I'm running on. I have it set to my local time zone. I also have

config.time_zone = 'Central Time (US & Canada)'
set in config/application.rb.

I want to be able to look at the raw database data in pgAdmin3 or some kind of database backup and know when records have been created in my time zone.

Any help would be appreciated. I will keep looking.

vee vee
Answer Source

If you want to set the db timezone and you are using ActiveRecord, then add the following in your application config:

config.active_record.default_timezone = :local

This will use your sever's timezone on the database.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download