user984621 user984621 - 17 days ago 5
MySQL Question

Rails - how to add timestamps to a model?

I have an exist model (and table) with these columns:

id
,
name
,
city
. In this table are data and I would like to this table add the columns
created_at
and
updated_at
- could anyone give me a help, how to do it? I mean - if I create a model, so these two columns are created automatically and the time informations are inserted always automatically, when I save there something.

Is possible now to add these two columns with automatic inserting time-data?

Answer

This should allow you to add timestamp columns to an already existing model

rake generate migration add_timestamps_to_users

This will create a migration file for you. Open it up and make necessary changes

class AddTimestampsToModel < ActiveRecord::Migration
  # in my example i'm using `users` table; change this to match your table name
  def change_table :users do |t|
    t.timestamps
  end
end

Then migrate your database

rake db:migrate
Comments