Change a column type from Date to DateTime during ROR migration
First in your terminal:
rails g migration change_date_format_in_my_table
Then in your migration file:
For Rails >= 3.2:
class ChangeDateFormatInMyTable < ActiveRecord::Migration
def up
change_column :my_table, :my_column, :datetime
end
def down
change_column :my_table, :my_column, :date
end
end
Also, if you're using Rails 3 or newer you don't have to use the up
and down
methods. You can just use change
:
class ChangeFormatInMyTable < ActiveRecord::Migration
def change
change_column :my_table, :my_column, :my_new_type
end
end
In Rails 3.2 and Rails 4, Benjamin's popular answer has a slightly different syntax.
First in your terminal:
$ rails g migration change_date_format_in_my_table
Then in your migration file:
class ChangeDateFormatInMyTable < ActiveRecord::Migration
def up
change_column :my_table, :my_column, :datetime
end
def down
change_column :my_table, :my_column, :date
end
end