A Beginner's Guide to Using MariaDB in a Ruby on Rails Application

In this guide, we will explore the basics of using MariaDB in a Ruby on Rails application, including installation, setup, and common database operations.

Posted on

MariaDB is a popular open-source relational database management system (RDBMS) that’s known for its performance, reliability, and ease of use. Ruby on Rails, often referred to as Rails, is a powerful web application framework that simplifies web development. In this guide, we will explore the basics of using MariaDB in a Ruby on Rails application, including installation, setup, and common database operations.

Prerequisites

Before we dive into using MariaDB with Ruby on Rails, make sure you have the following prerequisites in place:

  1. Ruby: You should have Ruby installed on your system. You can download it from the official Ruby website.

  2. Ruby on Rails: Install Ruby on Rails using the following command:

    gem install rails
    
  3. MariaDB: Install MariaDB if you haven’t already. You can download it from the MariaDB website.

  4. Database Adapter: Ensure you have the mysql2 gem installed, which serves as the database adapter for MariaDB:

    gem install mysql2
    

Setting Up a Rails Application

Now that you have the prerequisites in place, let’s create a new Ruby on Rails application that will use MariaDB as its database.

rails new my_rails_app -d mysql

In the above command:

  • my_rails_app is the name of your Rails application.
  • -d mysql specifies that we want to use the MySQL database adapter (mysql2) for MariaDB.

Configuring the Database

Next, navigate to your Rails application’s directory:

cd my_rails_app

Open the config/database.yml file. This file contains the database configuration for your application. Replace the default configuration with the following:

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: your_database_username
  password: your_database_password
  host: localhost

development:
  <<: *default
  database: myapp_development

test:
  <<: *default
  database: myapp_test

production:
  <<: *default
  database: myapp_production
  username: myapp
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>

Replace your_database_username and your_database_password with your MariaDB credentials. You can also adjust other settings as needed.

Database Migration

In Rails, database migrations are used to create and manage database tables and schema changes. Let’s create a simple example to demonstrate this:

rails generate model User name:string email:string

This command generates a User model with name and email attributes. Now, run the migration to create the corresponding table in the MariaDB database:

rails db:migrate

Performing Database Operations

With your Rails application set up and the database configured, you can now perform common database operations.

Inserting Data

To insert data into the users table, you can use the Rails console:

rails console

In the console, you can create and save new records:

user = User.new(name: 'John Doe', email: '[email protected]')
user.save

Querying Data

To query data from the users table, you can use Rails ActiveRecord queries. For example, to retrieve all users:

users = User.all

Updating Data

Updating data in Rails is straightforward. For example, to update a user’s email:

user = User.find_by(name: 'John Doe')
user.update(email: '[email protected]')

Deleting Data

To delete a record:

user = User.find_by(name: 'John Doe')
user.destroy

Handling Errors

In a real application, it’s crucial to handle errors gracefully. Ensure that you use error handling techniques, such as rescue blocks, to handle exceptions that may occur during database operations.

Conclusion

MariaDB is a robust and open-source RDBMS that pairs well with Ruby on Rails for web application development. In this guide, we’ve covered the basics of using MariaDB in a Ruby on Rails application, from installation and setup to common database operations. As you continue to develop your Rails application, you can explore more advanced features and optimizations provided by MariaDB to create efficient and scalable web applications.