Running Migrations in Rails: a cheatsheet

aaron feingold
2 min readFeb 2, 2021

First of all, what is Ruby-on-Rails? Simply, it’s a ruby gem, a code library, which is open source, and an MVC web framework. What’s great about that, is that using Rails, you won’t have create the base application functionality over and over again. So let’s jump into how to get started, and then run a migration (assuming you’re a Mac user).

Open a command terminal on you computer. Run the following:

gem install rails

Next generate a new rails app like this:

rails new your-apps-name

Notice the dashes between each word there, and it’s lowercase? That’s important. The name is used as a constant throughout the app, so KISS.

Now, make sure you enter the project’s folder:

 cd your-apps-name

Ok so now, if you want to see all the files and folders in your projects directory, simply type “ls” into your command terminal. There will be a bunch, and I’m not going to go over all of it here in detail. The Ruby docs have plenty info on that. But what we will do next is run bundle install to load in all our dependancies and then create a database like this:

bundle install
rake db:create

Next, we’ll go about this the manual way, which is not my preferred method. Ordinarily, I would run:

rails g migration create_users name email

for instance. However, instead, cd into db folder, then into the migrate folder that was created when we ran rails new. In that folder, create a new ruby file and name it something like 01_create_users.rb.

This is how we create our Users table. Make sense?

In this file, we create a ruby class that inherits from active_record:

Made it this far? Great, here’s the last step. In your terminal run:

rails db:migrate

and now the table has been migrated to our db, and we are ready to add some seeds to it, and once we’ve create our rails Models, we can perform various methods on our classes and instances.

The mechanics of a migration are complex. I hope this run down gives you a better idea of what is going on under the hood.

--

--

aaron feingold
0 Followers

web developer making apps for people with appetite