Integrating a Database to Your Ruby on Rails Application

Integrating a Database: An Essential Step

Congratulations on reaching this crucial stage of your journey in making your Ruby on Rails ToDo application enterprise-ready! In this lesson, you'll learn how to integrate a database into your application. If you've ever stored data manually in a file or used a spreadsheet for data tracking, then you can imagine the benefits of using a structured database to handle these tasks efficiently.

We'll start by showing you how to create and manage a database within a Rails application. This process allows you to store user data reliably and access it efficiently.

What You'll Learn

In this lesson, we will guide you through the creation of a database table specifically for storing ToDo items. You'll discover how to define the structure of your database using Rails migrations. Here is a brief look at the code that helps to achieve this:

class CreateTodos < ActiveRecord::Migration[6.1]
  def change
    create_table :todos do |t|
      t.string :title, default: 'Untitled'
      t.text :description

      t.timestamps
    end
  end
end

This code outlines a migration file, a core component of Rails that allows for managing the evolution of your database schema over time. In this example, we have set a default value of 'Untitled' for the title column, which will be used whenever a new record is created without specifying a title. Developers typically use a generator to create such migration files. For the CreateTodos migration, you can use the following Rails command:

rails generate migration CreateTodos title:string description:text

This command will automatically create a migration file with the structure for a todos table, including the specified title and description fields.

More on Generators

Once the migration file is generated, you need to apply it to update your database schema. Run the following command:

rails db:migrate

This will execute the migration, creating the todos table in your database with the specified columns. You will get hands-on experience creating a migration that builds a todos table with columns for a title and description as well as essential timestamp fields.

If you need to rollback or undo the migration, perhaps due to a mistake or a change in design, you can run:

rails db:rollback

This command will revert the last migration run, removing the todos table from the database. You can specify the step option to rollback multiple migrations:

rails db:rollback STEP=2

This will rollback the last two migrations. Understanding how to rollback migrations is crucial for efficiently managing changes to your database schema.

After applying your migrations to update the database schema, you might want to populate your database with initial data. Rails provides a convenient way to achieve this through seeding. The db/seeds.rb file allows you to define data that can be loaded into your database with a single command.

Here's an example of how you might seed your database with some initial ToDo items:

# db/seeds.rb
Todo.create(title: 'Buy groceries', description: 'Milk, Eggs, Bread, and Butter')
Todo.create(title: 'Read a book', description: 'Finish reading "Ruby on Rails Tutorial"')

To load the seed data, run the following command:

rails db:seed

Seeding your database is particularly useful for setting up development and test environments with baseline data, ensuring consistency and reliability as you build and test new features.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal