Elliot
Elliot

Reputation: 13845

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

When making my model, I made row type as string (I wasn't aware of datetime at the time). Currently I have plenty of records that have that row filled. Is there a safe way to convert the model's row to datetime through a migration - besides just removing it, and then adding it back?

Thanks!

Upvotes: 3

Views: 2823

Answers (2)

gicappa
gicappa

Reputation: 4882

Accordingly to the documentation this is an example of migration that transforms a string in datetime. I'm not sure it will work so you may want to try it out on a dev/stage env before going to the 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

Upvotes: 7

Jed Schneider
Jed Schneider

Reputation: 14679

I think what you are looking for is change_column

http://apidock.com/rails/v3.0.0/ActiveRecord/ConnectionAdapters/SchemaStatements/change_column

Upvotes: 3

Related Questions