Use `reset_column_information` to Migrate Data
If you're generating a Rails migration, chances are you might need to facilitate migrating data to a new column. You can use reset_column_information
in a migration file to pick up your changes and immediately do something with those new columns.
Assuming we have 2 models, DraftPost
and Post
-
class AddColumnDraftToPosts < ActiveRecord::Migration[5.2]
def change
add_column :posts, :draft, :boolean, default: true
Post.reset_column_information
DraftPost.all.each do |draft_post|
Post.create(content: draftPost.content)
draft_post.delete
end
end
Tweet