Talha Junaid
Talha Junaid

Reputation: 944

Rails 5: rename table migration

What is the best possible way to change name of table using migration and change name of all the files like controller, model and associations?

Will there be any issue when someone will try to run rails:db:migrate after cloning my repo?

Upvotes: 53

Views: 49820

Answers (1)

seancdavis
seancdavis

Reputation: 2821

What is the best possible way to change name of table using migration

To change the name of a table, you can run:

$ rails g migration change_[old_table_name]_to_[new_table_name]

Within the change method in the migration file generated, add this:

def change
  rename_table :[old_table_name], :[new_table_name]
end

Change [old_table_name] and [new_table_name] in both cases.

(This part of the question has been answered here.)

will there be any issue when someone will try to run rails db:migrate after cloning my repo?

Nope. Keep the old migration files in place and generate a new one. That's the benefit of database migrations.

What is the best possible way to change name of all the files like controller, model and associations?

It's generally not too big of a deal to change a model name. Many text editors have the ability to search and replace within a directory.

And I would manually rename the filenames.


Here's a set of more detailed steps to make sure you've hit everything that needs to be changed.

Upvotes: 109

Related Questions