Removing fields with a Rails migration

Ruby on Rails gives you powerful tools to make removing old fields easy, lets walk through how we can do this

Over time your Rails models will contain fields that no longer serve a purpose. To make things easier to maintain, you should remove fields that aren’t useful. Rails provides an easy way to do this.

In our imaginary system, we have the model Artist. When we first made the system, we thought that their eye colour would be a thing we needed. As the system matured, we realised that no one was using this attribute. After speaking to the stakeholders, we decide to remove eye_colour from our system.

In our terminal, we can type rails g migration RemoveEyeColourFromArtists eye_colour:string

rails g migration tells Rails to create a new migration for us. g is short for generate. If you prefer, you can write rails generate migration.

We start with the word Remove which tells Rails that this migration is about removing.

EyeColour is the field we will be dropping, this names the migration appropriately.

FromArtists lets Rails know the table we will be performing the migration on. Note the plural Artists, tables are always plural, to indicate they contain many of something.

Finally, we want to describe the field we are removing. It feels weird to define your attribute again, but migrations can have many actions inside them. eye_colour:string lets Rails know that the field we will be deleting is eye_colour and that it was a string.

We don’t need to tell Rails it was a string. It will delete any field so long as the name matches. What we gain by giving Rails this extra bit of information is the ability to undo the migration, more on that later.

When we run this command we get this as part of the output;

create    db/migrate/20200323105013_remove_eye_colour_from_artists.rb

All your migrations will live in db/migrate/.

The numbers are the year, month, day, hour, minute, and second we ran the command. This level of specificity helps reduce clashes and helps you to see when you made a migration.

remove_eye_colour_from_artists is what we typed in earlier, made lower case and with _ (snake case is the name for this).

Inside your new migration file, you will see a change method.

  def change
    remove_column :artists, :eye_colour, :string
  end

The method change gets ran when we do a rails db:migrate and also when we do rails db:rollback. When we do a rollback the opposite of what we ask happens. This is why we set string as the data type. When we do rails db:rollback Rails will know to recreate the field with the appropriate type.


Recent posts View all

Ruby

How to ignore Bullet in RSpec tests

Using Bullet during a test can pick up mistakes but also has false negatives; here is an easy way to ignore them

Ruby

How we used Nokogiri to fix a sizing issue in YouTube's oEmbed

At some point, one of our calls to YouTube's oEmbed endpoint was returning videos way too small; we fixed in in our Rails application by using Nokogiri