How to create a migration:
rails g migration migration_name
This will create an empty migration:
class <MigrationName> < ActiveRecord::Migration[5.0]
def change
end
end
An example of changing the column name from name to full_name. Here's the existing code for name:
class CreateProfiles < ActiveRecord::Migration
def change
create_table :profiles do |t|
t.references :user
t.string :avatar
t.string :name, null: false
end
end
end
And here's the change required to change from name to full_name:
class RenameProfileNameToFullName < ActiveRecord::Migration
def change
rename_column :profiles, :name, :full_name
end
end
Then run
rails db:migrate
To make the change to the database.
rails g migration rename_field
Which will create the migration file:
class UserAuth < ActiveRecord::Migration[5.2]
def change
rename_column :profiles, :name, :full_name
end
end