Created
April 22, 2014 17:13
-
-
Save suan/11187186 to your computer and use it in GitHub Desktop.
Rails timestamp with timezone investigation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rails 3 | |
class CreateFoo < ActiveRecord::Migration | |
def up | |
create_table :foos | |
execute "alter table foos add column created_at timestamp with time zone" | |
execute "alter table foos add column updated_at timestamp with time zone" | |
end | |
def down | |
drop_table :foos | |
end | |
end | |
# Rails 4 | |
class CreateFoo < ActiveRecord::Migration | |
def up | |
create_table :foos do |t| | |
t.column :created_at, "timestamp with time zone" | |
t.column :updated_at, "timestamp with time zone" | |
end | |
end | |
end | |
# IRB | |
1] pry(main)> Foo | |
=> Foo(id: integer, created_at: datetime, updated_at: datetime) | |
[2] pry(main)> f = Foo.new | |
=> #<Foo id: nil, created_at: nil, updated_at: nil> | |
[4] pry(main)> f.save! | |
(0.2ms) BEGIN | |
SQL (17.6ms) INSERT INTO "foos" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", Tue, 22 Apr 2014 11:52:43 CDT -05:00], ["updated_at", Tue, 22 Apr 2014 11:52:43 CDT -05:00]] | |
# PSQL from a Chicago session | |
netcredit_portfolio_development=# select * from foos; | |
id | created_at | updated_at | |
----+-------------------------------+------------------------------- | |
1 | 2014-04-22 11:52:43.273669-05 | 2014-04-22 11:52:43.273669-05 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment