Rails 3, changing a field in a model from string to datetime

When creating my model, I made the string type as a string (at that time I did not know about time and time). I currently have many entries that fill this line. Is there a safe way to convert a model string to datetime through migration - besides just deleting it and then adding it back?

Thank!

+3
source share
2 answers

According to the documentation, this is an example migration that converts a string to datetime. I'm not sure if this will work, so you can try it on dev / stage env before moving on to prod env.

class ChangeColumnToUsers < ActiveRecord::Migration
  def self.up
    change_column :users, :created_at, :datetime

  end

  def self.down
    change_column :users, :created_at, :string
  end
end
+6
source

Source: https://habr.com/ru/post/1784760/


All Articles