Puja
Puja

Reputation: 65

Trouble Handling DateTime fields in Rails 3.1

When a record is updated or created in MYSQL, the timestamp property stores the time and subtracts 0530hrs from the current system time. I want it to store the system time as is.

Below is one of such tables:

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string   :username
      t.string   :first_name
      t.string   :last_name
      t.timestamps 
    end
  end

t.timestamps creates two extra fields: created_at and updated_at. These fields are storing the time, which is not according to the system time.

Some data from the database

ID    ENo       Name   created_at                      updated_at
38    EMP1      Puja   2012-02-16 05:05:15.650740      2012-02-17 04:58:01.728120

The actual creation time was 10:35:15 AM on 16th feb 2012 and the update time was around 10:28 AM on 17th Feb.

What are the changes required to my application to achieve this?

Upvotes: 1

Views: 241

Answers (2)

sangeethkumar
sangeethkumar

Reputation: 821

Please update in config/environment.rb as

config.time_zone = 'IST' 

Upvotes: 0

wintersolutions
wintersolutions

Reputation: 5291

Rails always stores DateTime fields in UTC (Universal Time Coordinated). If you set the following option in the application.rb

config.time_zone = 'Central Time (US & Canada)' # UTC-5 

the dates will still be saved in UTC at the database level but will be translated when you access them.

Upvotes: 1

Related Questions