Year Wierdness

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

Here is the code that displays the date:

<% @blogs.each do |blog| %>
<%=h blog.postdate%>

<%=h blog.posttitle %>

<%=h blog.post %>

<%= link_to ‘Show’, blog %>
<%= link_to ‘Edit’, edit_blog_path(blog) %>
<%= link_to ‘Destroy’, blog, :confirm => ‘Are you sure?’, :method =>
:delete %>
<% end %>

Is there some sort of conversion that I am missing someplace?

  • Rilindo

You received this message because you are subscribed to the Google
Groups “Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

Rilindo F. 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.

Quoting Rilindo F. [email protected]:

<% end %>

Note: postdate column is a TIME (13:31:00), i.e. time of day. Use a
datetime
type in the database.

HTH,
Jeffrey

You received this message because you are subscribed to the Google
Groups “Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

On Dec 22, 2009, at 11:54 AM, Rilindo F. wrote:

±---------------------------
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…

=> :delete %>
To post to this group, send email to rubyonrails-
[email protected].
To unsubscribe from this group, send email to [email protected]
.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
.

You received this message because you are subscribed to the Google
Groups “Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

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!

On Dec 22, 2009, at 3:10 PM, Jeffrey L. Taylor wrote:

4 | 13:31:00 | Test again | again | 2009-12-22 18:32:06.800273 | 2009-12-22 18:32:06.800273
<%= link_to ‘Destroy’, blog, :confirm => ‘Are you sure?’, :method => :delete %>

You received this message because you are subscribed to the Google G. “Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

You received this message because you are subscribed to the Google
Groups “Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.