Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 904
user984621 Avatar asked Nov 24 '11 19:11

user984621


People also ask

What happens when you add a new model to a rails?

When you add a new model to a Rails application, it generates a migration to create the corresponding table for you. If that’s not what you need, you can write your own.

How to create user model with email field type in rails?

$ rails g model user email This command will generate user model with email field type of string, migration which creates users table, test for model and factory (if you have it). You are able to generate model with few fields like this: $ rails g model user first_name last_name email

How to add users to the Todo application in rails?

We want to add users to the todo application, so we need to add a username to each record. First, create a migration: As the command line implies, this tells Rails to generate a migration for us. It does all the work for us. Let’s run this and see what happens:

Are column changes reversible in rails?

Column changes, however, aren’t reversible. Adding a new class to an application is a frequent change too. When you add a new model to a Rails application, it generates a migration to create the corresponding table for you. If that’s not what you need, you can write your own.


2 Answers

It should be noted that while maček's answer is right at the core, he doesn't use the correct syntax for migrations (forgot the "change" method).

So here is his answer with corrected syntax:

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
    change_table :users do |t|
      t.timestamps
    end
  end
end

Then migrate your database

rake db:migrate
like image 62
exterm Avatar answered Oct 04 '22 20:10

exterm


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

rails generate migration add_timestamps_to_users

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

class AddTimestampsToUsers < 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
like image 43
maček Avatar answered Oct 04 '22 21:10

maček