Why is Rails displaying date like:
Sat Jan 01 13:31:00 UTC 2000
When the date is clearly correct in the database:
reverseblade_development=> select * from blogs;
id | postdate | posttitle | post | created_at | updated_at
Why is Rails displaying date like:
Sat Jan 01 13:31:00 UTC 2000
When the date is clearly correct in the database:
reverseblade_development=> select * from blogs;
id | postdate | posttitle | post | created_at | updated_at
Rilindo Foster wrote:
Why is Rails displaying date like:
Sat Jan 01 13:31:00 UTC 2000
When the date is clearly correct in the database:
reverseblade_development=> select * from blogs; id | postdate | posttitle | post | created_at | updated_at ----+----------+------------+-------+----------------------------+---------------------------- 4 | 13:31:00 | Test again | again | 2009-12-22 18:32:06.800273 | 2009-12-22 18:32:06.800273
Your 'postdate' is stored as ... Just a time! So Rails defaults to Jan. 01, 2000. You may want to do a migration and change your column to a datetime... ? As usual, beware of modifying pre-existing data.
Why is Rails displaying date like:
Sat Jan 01 13:31:00 UTC 2000
When the date is clearly correct in the database:
reverseblade_development=> select * from blogs; id | postdate | posttitle | post | created_at > updated_at ----+----------+------------+-------+----------------------------+---------------------------- 4 | 13:31:00 | Test again | again | 2009-12-22 18:32:06.800273 | 2009-12-22 18:32:06.800273
postdate doesn't look like a date to me. It looks like a time...
Quoting Rilindo Foster <rilindo@gmail.com>:
Why is Rails displaying date like:
Sat Jan 01 13:31:00 UTC 2000
When the date is clearly correct in the database:
reverseblade_development=> select * from blogs; id | postdate | posttitle | post | created_at | updated_at
----+----------+------------+-------+----------------------------+---------------------------- 4 | 13:31:00 | Test again | again | 2009-12-22 18:32:06.800273 | 2009-12-22 18:32:06.800273Here is the code that displays the date:
<% @blogs.each do |blog| %> <%=h blog.postdate%><br> <%=h blog.posttitle %><br> <%=h blog.post %><br> <%= link_to 'Show', blog %> <%= link_to 'Edit', edit_blog_path(blog) %> <%= link_to 'Destroy', blog, :confirm => 'Are you sure?', :method => :delete %> <% end %>
Note: postdate column is a TIME (13:31:00), i.e. time of day. Use a datetime type in the database.
HTH, Jeffrey
Oh my goodness you're right.
Unfortunately, I couldn't seem to change the column type gracefully with postgres:
(in /Users/rilindo/src/rrproj/reverseblade) == ModifyPostdateColumn: migrating =========================================== -- change_column(:blogs, :postdate, :datetime) rake aborted! An error has occurred, this and all later migrations canceled:
PGError: ERROR: column "postdate" cannot be cast to type "pg_catalog.timestamp" : ALTER TABLE "blogs" ALTER COLUMN "postdate" TYPE timestamp
(See full trace by running task with --trace) tristan:reverseblade rilindo$ rake db:migrate (in /Users/rilindo/src/rrproj/reverseblade) == ModifyPostdateColumn: migrating =========================================== -- change_column(:blogs, :postdate, :datetime) rake aborted! An error has occurred, this and all later migrations canceled:
PGError: ERROR: column "postdate" cannot be cast to type "pg_catalog.timestamp" : ALTER TABLE "blogs" ALTER COLUMN "postdate" TYPE timestamp
I ended up having to rename the column and added a new one:
class ModifyPostdateColumn < ActiveRecord::Migration def self.up rename_column :blogs, :postdate, :old_postdate add_column :blogs, :postdate, :datetime end
def self.down remove_column :blogs, :postdate rename_column :blogs, :old_postdate, :postdate end end
Now It is displaying the right date. Thanks!