How to rename column name in Rails


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.




Discover More Insights: Explore Our Recommended Posts!