Day 3: How Rails Works – Database Migrations

How Rails works

Here is what happens when you go to your browser and see a web page.

How Rails Works Rails is a MVC framework. MVC stands for Model View Controller. Model, View and Controller components are displayed as scroll scripts on the picture above. Think about each of the components as of individual ruby files.

As soon as you hit enter after typing in the URL, your browser sends a request to a server. In our case the server is running locally. We stated it with bundle exec rails server command.

When request hits the server, controller takes care of it. It reads the url and tries to understand what kind of a page you want to see. Then it usually instantiates a model. Model retrieves data from a database. The data is returned to the controller. Now the controller has all data it needs, it uses a view component to construct a HTML page. Finally, HTML is generated and controller sends the response back to the browser.

Don’t worry if it sounds confusing. It will make more sense when we’ll write code for every component. Let’s do it!

Database migrations

To keep things simple, let’s assume that a task can be either completed or incomplete. In order to capture this information we need to store it inside the database. Rails uses the mechanism called migrations. It’s really simple. You need to generate a migration and run it. Run this command in the terminal to generate it.

Generate a Migration with Ruby on Rails

Look at the output. It have generated a file for us. It’s located under db/migrate folder. Let’s open this newly generated file in our editor. Launch RubyMine application. Click Open Directory link and choose the project location (it should be in home -> todo).

Open Directory in RubyMine

Next, look at the directory structure on a left. Navigate to db/migrate folder and choose the generated migration file.

Open File in RubyMine

This is the generated class AddCompletedToTasks. As you can see it inherits from the base class ActiveRecord::Migration. For now it has only one method called change which does nothing. Let’s write the code that adds a completed column to Tasks table.

Note: Double colon sign :: is used as a namespace separator.

class AddCompletedToTasks < ActiveRecord::Migration
  def change
    add_column :tasks, :completed, :boolean
  end
end

We used the function add_column. Where does this function come from ? Right, it’s inherited from ActiveRecord::Migration base class.

Note: We passed input parameters into add_column function. Here we used the new data type called symbols. Symbols are similar to strings. The main difference is that symbols are immutable. You can’t change symbols after you created them. Therefore, symbols work well as input parameters. The way you define a symbol is by prefixing the name of the symbol with a colon.

We wrote our migration. Let’s run it.

$ bundle exec rake db:migrate
== AddCompletedToTasks: migrating ===============================================
-- add_column(:tasks, :completed, :boolean)
-> 0.0043s
== AddCompletedToTasks: migrated (0.0044s) ======================================

As you can see from the terminal output, we’ve successfully added the new completed column. Awesome! Let’s move on to the Task model. Click here to go to Day 4: Models, Controllers and Routing

Cover photo by victorcamilo