January 12, 2006

Posted by John

Tagged timestamps

Older: Welcome to railstips.org!

Newer: A roundup of the best Rails tutorials

Populating timestamps with ease

I’ll start off the tips with something that I didn’t pay attention to the first time I was putting together a Rails app.

If you have a timestamp or datetime column your database table, you can let ActiveRecord take care of populating it:

> Active Records will automatically record creation and/or update timestamps of database objects if fields of the names created\at/created\on or updated\at/updated\on are present. This module is automatically included, so you don’t need to do that manually.
> This behavior can be turned off by setting ActiveRecord::Base.record\
timestamps = false. This behavior can use GMT by setting ActiveRecord::Base.timestamps\
gmt = true

I overlooked this in my first application and named my timestamp columns something like “date_added,” and then had to specify how to set the time. I’ve since learned my lesson!


Thoughts? Do Tell...

textile enabled, preview above, please be nice
use <pre><code class="ruby"></code></pre> for code blocks


Authored by John Nunemaker (Noo-neh-maker), a programmer who has fallen deeply in love with Ruby. Learn More.


Release your software more often with fewer problems.
Flip your features.