I need to add timestamps (created_at updated_at) to an existing table. I tried the following code but it didn't work. I have also tried other solutions I found online but they don't work either.
class AddTimestampsToUser < ActiveRecord::Migration
def change_table
add_timestamps(:users)
end
end
How can I do it?
I made a simple function that you can call to add to each table (assuming you have a existing database) the created_at and updated_at fields:
Your original code is very close to right, you just need to use a different method name. If you're using Rails 3.1 or later, you need to define a
change
method instead ofchange_table
:If you're using an older version you need to define
up
anddown
methods instead ofchange_table
: