How to rename column name in Rails


Ruby On Rails - SapidLabs

Rails rename_column

Rails provides a simple method rename_column which can be used in a migration.


Example: Say you have a User model in your rails application, with a db field as name. Now, you want to change the name of the column. we should generate a migration by using rails g migration change_users_name_to_title. Our migration should look like:

class ChangeUsersNameToTitle < ActiveRecord::Migration[6.0]
  def change
    rename_column :users, :name, :title
  end
end

Now run rails db:migrate in order to make the changes to the database.

## Rename column method
rename_column(:table_name, :column_name, :new_column_name)


Verify from the schema.rb to see if the field actually changed.


Recommended Posts

Default Enum in Rails 6
Rails 6 provides a way to define default enum value. Let’s see how it works...
Ruby 2.7 adds Enumerable#tally
After introducing the Enumerable#tally method in Ruby 2.7-preview updates. #tally as the name suggests gives...
How to use Enum in Rails
Enum in Rails “Enum” for short for the word “Enumerator” is widely used in different...