DEV Community

Cover image for Migrating in Ruby
paolomgarcia
paolomgarcia

Posted on

Migrating in Ruby

Things switch up quickly when you get to the back-end of code. Everything that gave you immediate response is no longer, and the puzzle solving begins. I actually felt like I tied everything in a lot better with the back-end (although I can't wait to learn a language outside of Ruby). The creative leaning front-end is something that truly pushes me outside of my comfort zone and still is largely rewarding to pull off; but the back-end just clicks. It is tricky, but direct. It is lengthy, but necessary. Without a back-end, a website is generally forever stagnant.

So, I'll go over the basic instructions to get through a simple migration on Ruby. Ruby runs entirely through the terminal, and you have to make sure all your steps are taken in correct order or your seeds and migrations will not be correct.

Go ahead and install your bundles and begin with the following:

rake db:create_migration NAME _________

  • note: the "NAME" is optional and you can jump direction from create_migration to whatever you want to name that migration.

After creating your migrations, you're going to want to go into those files and create your migration tables inside. This will look something like the following:

class CreateStudents < ActiveRecord::Migration[6.1]
   def change
      create_table :table_name do |t|
         t.string :color
         t.integer :length
         t.boolean :finished?
Enter fullscreen mode Exit fullscreen mode

Once finished with the creation of your migration tables, go back to your terminal and type:

rake db:migrate

This will create a schema folder with all of your migrations and a seed folder that you will need to bring in your database information to. This could look as the following:

Model.create(color:"brown", length:7, finished?: FALSE)
Enter fullscreen mode Exit fullscreen mode

You will want to do this to pass the migration information to every model that you have passing that information through.

I hope this helps any Ruby novice get through a migration as easily as possible!

Top comments (0)