How to Rename a Column in Ruby on Rails

Feature thumb snip20200728 13

The Ruby on Rails framework has some helpful shorthand methods for when you want to change the structure of your database. If you want to rename a column, you can create a migration with the terminal command:

rails g migration change_some_name

And then the code in the migration file should look something like this:

class ChangeSomeName < ActiveRecord::Migration[6.0]
  def change
    rename_column :table_name, :old_name, :new_name

After you run: rails db:migrate, the new name will be updated in the database. A few things to keep in mind:

Make sure to update any/all mentions of the old name in the: strong params, serializers, seed files, show templates, etc. Ensure that the table name you're using matches the table name as it appears in the db/schema.rb file

For more information on rails migrations, the guide below is very helpful.