How to rename a column in Rails 5

Want to run a migration to rename a column in Rails 5? First, run:

rails g migration ChangeColumnName

Next, open the resulting migration and add the rename_column line below. Make sure to change table_name, old_column, and new_column to your own values.

table_name should be plural.

class ChangeColumnName < ActiveRecord::Migration[5.2]
def change
rename_column :table_name, :old_column, :new_column

Now, run rails db:migrate.

About the author

I'm Mark Thomas Miller, a full stack engineer and designer currently working at ConvertKit. (We're hiring!) People like Arnold Schwarzenegger, Lindsey Stirling, and Tim Ferriss use features I've built to connect with their fans. I'm currently geeking out about Svelte, mechanical keyboards, and minimalist UI design, and replaying Ocarina of Time.