Process of migration not in a bird way

KennethLatourII
3 min readOct 3, 2022

Migrations can seem daunting when first tackled, but don’t worry, as a step-by-step guide will get your tables up and running in no time. Before we go any further in the application, let’s make sure that all the gems required for the project are installed by running

Excellent, now we can move forward in our migrations. The next step is actually to create said migrations. We do this by using the terminal for the shortest method. Be careful. The syntax must be correct in the next few steps.

This will practically do all the work for you. Let’s point out some common gotchas associated with this command. Makes sure the NAME=create_pets is all one word; this assures that the file that rake will create has the naming taxonomy we desire. Pluralization is critical to cohesively use ActiveRecord the table must be the plural form of the ruby files in our app. This other one is optional, but the best practice is to bundle exec before the rake. This allows rake to interact no matter what version is installed, even if it is a later or newer versions.

Now we should see a migration in our db →migrate folder. It will have a long number before the name is specified. This is a time stamp telling rake which ones to run first. The earlier you create a migration, the higher it will be in the running list. This is an important note once you start associating tables. Once the table is filled out, remember to use the pluralization of our ruby file. We then can migrate said table to integrate it into our code. By utilizing a rake command in our terminal, we can do this.

Suppose all went well, a schema.rb should’ve been created with your table in the db foulder. Do not edit the schema for our purposes. Never touch the schema unless you want a headache; we can use a couple of commands if mistakes were made in our table.

These are all valid options. The first will just rollback your first migration. The second will rollback as many as you specify after the STEP=. The last is the least recommended, but it will drop all migrations. You will have to delete your schema and then can edit your table. After all these, you must migrate again once you have fixed your error. Good luck and happy coding.

--

--