[Ruby] [Ruby on Rails] How to change column name

less than 1 minute read


environment ruby-2.6.3

During the development process, I had to change the column name. I will write down the coping method as a memorandum.

Thing you want to do

Change the column name of model user as follows

zipCode → zip_code


1. Create migration file

Run the following command db/migrate/20200825114039_rename_zip_code_column_to_users.rb Create a.

rails generate migration rename_zipCode_to_users

2. Edit the migration file


class RenameZipCodeColumnToUsers <ActiveRecord::Migration[6.0]
  def change
    rename_column :users, :zipCode, :zip_code

3. Reflect to database

Do the following

$ rake db:migrate

The column name of the users model has been changed from zipCode to zip_code.

At the end

Initially after rewriting the column name with code I thought I should do “rake db:migrate”, but it wasn’t.

It’s a bit tedious to get used to, but even if you make similar mistakes in the future It seems that you can calmly deal with it.