B Seven
B Seven

Reputation: 45943

How to add created_at and updated_at when creating a table with a Rails migration

Rails g migration CreateFoo

This does not add created_at and update_at to the migration. Is there a way to generate a migration and add timestamps as a param?

Rails g migration CreateFoo timestamps didn't work.

Upvotes: 3

Views: 9559

Answers (3)

Gupta
Gupta

Reputation: 10358

You need to destroy that table and generate again

Rails g migration CreateFoo

and check the generated migration file whether timestamp mehtods is there or not. if not then put like this

t.timestamps

Edit update

You could use either syntax.

t.datetime :created_at
t.datetime :updated_at

t.timestamps is just an abstract form of writing.

Hope it will help.

Upvotes: 7

user3506853
user3506853

Reputation: 814

You can add timestamps by doing this :-

change_table :table_name do |t|
  t.timestamps
end

Upvotes: 2

TK-421
TK-421

Reputation: 10763

Seems like those only get generated automatically with models.

Otherwise you can add it manually in a create with t.timestamps or with add_timestamps :foo to change an existing table.

Upvotes: 5

Related Questions