MariaDB CRUD Tutorials in Ruby/Rails: A Step-by-Step Guide

In this tutorial, we will explore how to perform CRUD (Create, Read, Update, Delete) operations with MariaDB using Ruby on Rails.

Posted on

MariaDB is a popular open-source relational database management system, and Ruby on Rails is a powerful web application framework. In this tutorial, we will explore how to perform CRUD (Create, Read, Update, Delete) operations with MariaDB using Ruby on Rails. By the end of this article, you’ll have a solid understanding of how to interact with a MariaDB database from your Ruby on Rails applications.

Prerequisites

  1. Ruby installed on your machine.
  2. Ruby on Rails installed on your machine.
  3. A running MariaDB server.
  4. Basic knowledge of Ruby and Ruby on Rails.

Step 1: Create a New Ruby on Rails Application

Let’s start by creating a new Ruby on Rails application. Open your terminal and run the following command:

rails new mariadb_crud_app

This command will create a new Rails application named mariadb_crud_app. Navigate to the project directory using cd mariadb_crud_app.

Step 2: Configure the Database Connection

In your Rails application, you need to configure the database connection to MariaDB. Open the config/database.yml file and make sure it looks like this:

default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: your-username
  password: your-password
  host: your-database-host
  database: your-database-name

development:
  <<: *default

test:
  <<: *default

production:
  <<: *default

Replace 'your-username', 'your-password', 'your-database-host', and 'your-database-name' with your MariaDB server details.

Step 3: Generate a Model and Migration

Now, let’s generate a model and a corresponding migration for our CRUD operations. In your terminal, run the following command:

rails generate model User name:string email:string

This command generates a User model with name and email fields.

Next, apply the migration to create the users table in your MariaDB database:

rails db:migrate

Step 4: Create a Controller

Let’s create a controller to handle our CRUD operations. Run the following command to generate a UsersController:

rails generate controller Users

This command generates a controller file (users_controller.rb) and views for the Users resource.

Step 5: Implement CRUD Operations

Create (C)

In your users_controller.rb file, add the following code to create a new user:

def create
  @user = User.new(user_params)

  if @user.save
    redirect_to users_path, notice: 'User was successfully created.'
  else
    render :new
  end
end

This code defines a create action that creates a new user record in the users table.

Read (R)

To retrieve and display user records, add the following code to your index action:

def index
  @users = User.all
end

This code fetches all user records from the users table and stores them in the @users instance variable.

Update (U)

For updating user records, add the following code to your edit and update actions:

def edit
  @user = User.find(params[:id])
end

def update
  @user = User.find(params[:id])

  if @user.update(user_params)
    redirect_to users_path, notice: 'User was successfully updated.'
  else
    render :edit
  end
end

These actions allow you to edit and update user records.

Delete (D)

To delete user records, add the following code to your destroy action:

def destroy
  @user = User.find(params[:id])
  @user.destroy

  redirect_to users_path, notice: 'User was successfully deleted.'
end

This action deletes a user record.

Step 6: Create Views

Create views to display and interact with your users. In the app/views/users folder, create the following files:

  • index.html.erb: Display a list of users.
  • new.html.erb: Create a new user.
  • edit.html.erb: Edit an existing user.

Here’s an example index.html.erb file:

<h1>Users</h1>

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Email</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @users.each do |user| %>
      <tr>
        <td><%= user.name %></td>
        <td><%= user.email %></td>
        <td><%= link_to 'Show', user %></td>
        <td><%= link_to 'Edit', edit_user_path(user) %></td>
        <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New User', new_user_path %>

Step 7: Run Your Application

Start your Rails server by running:

rails server

Visit http://localhost:3000/users in your web browser to interact with your CRUD application for MariaDB.

Conclusion

In this article, we’ve covered how to perform CRUD operations with MariaDB using Ruby on Rails. You’ve learned how to create a Rails application, configure the database connection, generate models, controllers, and views, and implement the CRUD operations for managing user records. This knowledge will empower you to build more complex web applications that interact with MariaDB databases seamlessly. Remember to handle errors gracefully and adapt these examples to suit your specific project requirements.