Jackie Chan
Jackie Chan

Reputation: 2662

created_at is different from DateTime.now

Got into a really sticky situation, while saving a model instance I get a value of created_at dated 9 hours ago.

While executing DateTime.now I get exactly the NOW time, as well as when I execute NOW() and current_timestamp on SQL server.

So my question would be:

  1. Is it possible to override the default created_at setter method? Or change the timestamp setter? Or how does rails handle setting the create_at method?
  2. Is there any config setting settings? Initializers, or anything that might point me to the right direction of setting the created_at method?

Any help would be appreciated

Upvotes: 1

Views: 1903

Answers (1)

rmagnum2002
rmagnum2002

Reputation: 11421

http://www.ruby-doc.org/core-2.0.0/Time.html#method-c-now

Time.now - Returns a Time object initialized to the current system time.

created_at - is saved the GMT time, in my case it shows 10.00 for example but my system time is 12.00 as I am 2 hours ahead of GMT.

Set the TimeZone in environment.rb:

# config/environment.rb
config.time_zone = "Central Time (US & Canada)"

Run rake time:zones:all to get the actual listing and use the one you need, like Central Time (US & Canada).

Upvotes: 2

Related Questions