Tutorials

Rename an ActiveRecord model

Choosing a good name for a class or a variable isn’t easy at all and sometimes there is a requirement to rename existing tables and models to match the updated product audience.

So how we should do this migration then:

1- Ask why

The first step is to ask why should we rename the model, with a productive mindset and a good set of questions you will be surprised that in most cases a renaming is not required at all!

2- Change the model name

It seems that this case requires to rename the ActiveRecord model, so let’s use the following example:

As we can see we have an Author model who has many Books and our requirement is to rename Author model to Writer.

We start by renaming the Author model file and mapping the new model to the existing authors table. Also, we need to specify the foreign key for the association to be able to access associated books.

Let’s update the Book model by specifying the existent foreign key. This step is important to maintain the belongs_to association.

I guess that you are asking why should we bother with this step instead of renaming the models and tables at the same time.

To answer this good question, we should consider how applications look like in production. Usually, models may have different associations and used in different parts of the application like controllers and services. That’s why we do this process step by step to be sure that our application is still working as expected before changing the tables and columns in the database.

3- Run the test suite

At this point, we must run our test suite and be sure all specs are passing before moving to the last step. This step is very important to be sure that we didn’t break an existent feature during the migration.

4- Rename DB tables and columns

Now we are ready for the last step, so let’s write some migrations. We start by renaming the authors‘ table to writers.

Then we need to rename the foreign key from author_id to writer_id.

And finally, we clean up our models.

Here is the updated schema, we can check that renaming the column took care of renaming the index. But we should keep in mind this only valid for Rails version >= 4.0 as stated in this changelog:

In Rails 4.0 when a column or a table is renamed the related indexes are also renamed. If you have migrations which rename the indexes, they are no longer needed.

source

That’s it, have fun and keep coding 🙂