Lesson 1
Integrating a Database to Your App
Introduction

Welcome to the first lesson of our course, "Adding Enterprise Features to Your Ruby on Rails App". In this lesson, we will focus on integrating a database into your Ruby on Rails application. Understanding how databases interact with your web application is crucial because databases store the data necessary for your application to function.

In Ruby on Rails, a common way to handle database changes is through migrations. Migrations are a convenient way to alter your database schema over time in a consistent and easy way. This lesson will guide you through creating your first migration to add a todos table to your database.

Understanding Migrations

A migration in Ruby on Rails is a way to change your database schema over time. It allows you to describe changes in a Ruby DSL, which is useful for version control and collaboration.

In simpler terms, migrations are like versions of your database. Each migration describes a change in the database schema, such as adding a table or altering columns. Using migrations ensures that these changes can be easily replicated in different environments by running simple Rails commands.

Migrations are essential because they:

  1. Provide a structured and organized way to manage database changes.
  2. Ensure that all team members can apply the same database changes.
  3. Help track the history of changes to the database.
Creating a Migration for ToDo Items

Now, let's create a migration to add a todos table to our database. Rails makes it easy to create a migration with a simple command:

Bash
1# Terminal Command 2rails generate migration CreateTodos

The name CreateTodos is descriptive and follows a Rails convention by clearly indicating that this migration is intended to create the todos table. This command generates a new migration file in the db/migrate directory with a name following the YYYYMMDDHHMMSS_create_todos.rb convention. Here, YYYYMMDDHHMMSS is a timestamp representing the exact time the migration was created, ensuring that migrations are run in the correct order. You will need to edit this file to define your table and its columns. Here’s an example of what the code should look like in our migration:

Ruby
1class CreateTodos < ActiveRecord::Migration[7.1] 2 def change 3 create_table :todos do |t| 4 t.string :title 5 t.text :description 6 7 t.timestamps 8 end 9 end 10end
Breaking Down the Migration Code

Let's break down the migration code step by step to understand what each part does:

Ruby
1class CreateTodos < ActiveRecord::Migration[7.1] 2 def change

This line defines a new class CreateTodos that inherits from ActiveRecord::Migration, a Rails component that provides a Ruby DSL to define and manage version-controlled database schema changes. The change method will describe the changes to be made to the database.

Ruby
1 create_table :todos do |t|

create_table :todos is a method that creates a new table named todos. The block variable t will be used to define the columns of the table.

Ruby
1 t.string :title 2 t.text :description
  • t.string :title creates a column named title of type string.
  • t.text :description creates a column named description of type text.
Ruby
1 t.timestamps

t.timestamps adds two special columns, created_at and updated_at, which automatically record when a row is created and last updated.

Running the Migration

Once the migration file is ready, you need to run it to apply the changes to the database. You can do this with the following command:

Bash
1# Terminal Command 2rails db:migrate

This command runs all pending migrations. If successful, you will see output that confirms the creation of the todos table:

1== 20230101000000 CreateTodos: migrating ====================================== 2-- create_table(:todos) 3 -> 0.0012s 4== 20230101000000 CreateTodos: migrated (0.0020s) =============================
Summary and Next Steps

To recap, in this lesson, you learned about the importance of databases and migrations in Ruby on Rails. We went through the initial setup of the Rails app, created a migration to add a todos table, and ran the migration to modify the database schema.

As you proceed with these exercises, you'll get more hands-on experience, reinforcing what you've learned and setting the foundation for adding more enterprise features to your ToDo app in the upcoming lessons. Great job getting to this point!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.