MySQL 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 MySQL using Ruby on Rails.

Posted on

MySQL is a widely-used 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 MySQL using Ruby on Rails. By the end of this article, you’ll have a solid understanding of how to interact with a MySQL 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 MySQL 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 mysqlcrudapp

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

Step 2: Configure MySQL Connection

In your Rails application, you need to configure the connection to MySQL. 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

Replace 'your-username', 'your-password', 'your-database-host', and 'your-database-name' with your MySQL 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 Article title:string content:text

This command generates an Article model with title and content fields.

Next, apply the migration to create the articles table in your MySQL database:

rails db:migrate

Step 4: Implement CRUD Operations

Create (C)

In your articles_controller.rb file, add the following code to create a new article:

def create
  @article = Article.new(article_params)

  if @article.save
    redirect_to @article, notice: 'Article was successfully created.'
  else
    render :new
  end
end

This code defines a create action that creates a new article record in the articles table.

Read (R)

To retrieve and display article records, add the following code to your index and show actions:

def index
  @articles = Article.all
end

def show
  @article = Article.find(params[:id])
end

These actions allow you to display a list of articles and view individual articles.

Update (U)

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

def edit
  @article = Article.find(params[:id])
end

def update
  @article = Article.find(params[:id])

  if @article.update(article_params)
    redirect_to @article, notice: 'Article was successfully updated.'
  else
    render :edit
  end
end

These actions allow you to edit and update article records.

Delete (D)

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

def destroy
  @article = Article.find(params[:id])
  @article.destroy

  redirect_to articles_path, notice: 'Article was successfully deleted.'
end

This action deletes an article record.

Step 5: Create Views

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

  • index.html.erb: Display a list of articles.
  • show.html.erb: Display an individual article.
  • new.html.erb: Create a new article.
  • edit.html.erb: Edit an existing article.

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

<h1>Articles</h1>

<ul>
  <% @articles.each do |article| %>
    <li>
      <%= link_to article.title, article %>
    </li>
  <% end %>
</ul>

<%= link_to 'New Article', new_article_path %>

Step 6: Run Your Application

Start your Rails server by running:

rails server

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

Conclusion

In this article, we’ve covered how to perform CRUD operations with MySQL using Ruby on Rails. You’ve learned how to create a Rails application, configure the MySQL connection, generate models, controllers, and views, and implement the CRUD operations for managing article records. MySQL is a widely-used relational database, and integrating it with Ruby on Rails allows you to build robust web applications. Remember to handle errors gracefully and adapt these examples to suit your specific project requirements.