Forum: Ruby on Rails on_update current_timestamp in Migrations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F70a33e0a37977db83bc30befd66a902?d=identicon&s=25 ar k. (kortina)
on 2006-06-12 23:00
Is there any way to set database columns to auto update a timestamp
column on update?
Can I do something like
      t.column "timestamp", :datetime, :default => :current_time, :null
=> false, :on_update => :current_timestamp

or do i need to just use custom sql like

sql = "ALTER TABLE `table` CHANGE `timestamp` `timestamp` TIMESTAMP ON
UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP"

exectue sql

Any help is appreciated.  Thanks.
Dac506f50e53f5a2f7c7be3f257ed5dc?d=identicon&s=25 Danny B. (dburkes)
on 2006-06-12 23:11
> Is there any way to set database columns to auto update a timestamp
> column on update?

If you simply name your column 'updated_at', Rails will update it
automatically when its parent record is updated.

FYI, 'updated_at' is a magic column name(tm).  For some others, see

http://wiki.rubyonrails.com/rails/pages/MagicFieldNames

- Danny
F70a33e0a37977db83bc30befd66a902?d=identicon&s=25 ar k. (kortina)
on 2006-06-12 23:22
Daniel Burkes wrote:
>> Is there any way to set database columns to auto update a timestamp
>> column on update?
>
> If you simply name your column 'updated_at', Rails will update it
> automatically when its parent record is updated.
>
> FYI, 'updated_at' is a magic column name(tm).  For some others, see
>
> http://wiki.rubyonrails.com/rails/pages/MagicFieldNames
>
> - Danny

Wow, that's excellent.  The more I learn about rails, the more I love
it.  Thank you!
This topic is locked and can not be replied to.