Active Records? Migrations? What?

Storm Sarracino
4 min readDec 7, 2021

What is Active Record?

Active Record is an ORM (Object-Relational Mapping), which is just the technique of accessing a relational database using an object-oriented programming language; in this case, it’s Ruby. ORM is just a design pattern that gives us a way to organize our programs when we want to connect a class to a relational database. We use ORM to cut down on repetitive code and implement conventional patterns that are organized and sensical. Imagine having more than a single class. Building a database that connects each class to it as well as making sure everything works is going to take a lot more time and takes away the focus of building the entire application. This is where Active Record comes in. Active Record is a Ruby gem, which means you get a whole library of code. When you have more than one class or a different database, Active Record will smooth out all the differences and will take care of connecting everything. What exactly does this mean? Well, this means that Active Record can represent models and their data, represent associations between these models, represent inheritance hierarchies through related models, validate models before they get persisted to the database, and perform database operations in an object-oriented fashion.

What are Migrations?

Migrations are files that describe changes made to our database structure. We use migrations to make managing our database easier over time. Migrations make reversing changes easier and collaborations with other people. Instead of writing schema modifications in pure SQL, migrations allow you to use a Ruby DSL (Domain Specific Language). This allows us to only need to make a change in one place and that change will be pushed through the entire code. Migrations are like a control system for your database.

What is Rake?

Rake is an awesome tool that you can use with Ruby projects. It allows you to use Ruby code to define “tasks” that can be run in the command line. Rake tasks are similar to the things like “npm start” or “npm test” in JavaScript. Rake allows us to have a convenient way of defining and executing tasks using Ruby. This takes away the chore of creating or maintaining a database table or having to think about assigning what will be responsible for doing a certain task.

Creating Migrations.

When it’s time to create a migration the command that you’ll enter in the terminal is “rake db:create_migration NAME=name_of_things”. A file will be created with a timestamp at the beginning of the file. It’s important to note that the rake command the created the table will make sure that the proper naming conventions are used. Without the proper naming conventions, an error will occur.

Next thing to do will be to actually start making the table. In your file you’ll have a method called change, this is where you want to create the table. You’ll be creating columns that are going to be integers, strings, or booleans and then naming these columns to the right side of them.

After creating the table you’ll want to migrate them. In your terminal the command you’ll type in “rake db:migrate” and this will create a schema file with identical data of your migrations. You will also get a printout in the terminal saying “name_of_tables is created”. In your schema file, you’ll see something about a version. This version is what Active Record uses to keep track of the data. This just means that if you were to run “rake db:migrate” in the terminal again nothing will happen because it’s still the same version as when you last ran that command. Nothing was changed so Active Record doesn’t have anything to update.

Now say that you want to make a change to your migration. In your terminal type in “rake db:rollback” and you’ll see something about a drop table. Then you make whatever change you wanted to make. After this, you’re going to want to run the migration again by typing in “rake db:migrate” in the terminal. Once you do this you’ll see the changes that were made in the schema file and also a printout in the terminal saying the table was created.

Now there are more commands you can use but these are the main 3 you’ll probably use the most. If you want to see more commands you can type in “rake -T” in the terminal and it’ll print out the command and a brief explanation of what it does.

Each programming language will have its own cool tools that are available to use. Ruby is no different in that aspect. The tools are there to help and make coding easier and less time-consuming.

--

--