0

I am new to Rails. Facing issue while changing table name.

I tried changing the name by going to create_table migration file and changed name from there but it didn't work.

Eyeslandic
  • 14,553
  • 13
  • 41
  • 54
  • Ref: https://stackoverflow.com/a/471425/13841038 – Manjunath P Oct 07 '21 at 15:12
  • Does this answer your question? [How do you write a migration to rename an ActiveRecord model and its table in Rails?](https://stackoverflow.com/questions/471416/how-do-you-write-a-migration-to-rename-an-activerecord-model-and-its-table-in-ra) – Eyeslandic Oct 07 '21 at 17:35

2 Answers2

5

First you need to generate migration for renaming

$ rails g migration RenameOldTableToNewTable

Then inside the newly created migration file you should add rename_table statement

class RenameOldTableToNewTable < ActiveRecord::Migration[6.1]
  def change
    rename_table :old_table_name, :new_table_name
  end 
end

Finally run migrations

$ rails db:migrate

Ref: https://stackoverflow.com/a/471425/13841038

Manjunath P
  • 403
  • 3
  • 10
2

You can create a new migration file for changing the table name and use rename_table command like this:

rename_table :old_name, :new_name
Sachin Singh
  • 993
  • 8
  • 16