PostgreSQL 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 PostgreSQL using Ruby on Rails.
PostgreSQL is a powerful open-source relational database management system, and Ruby on Rails is a versatile web application framework. In this tutorial, we will explore how to perform CRUD (Create, Read, Update, Delete) operations with PostgreSQL using Ruby on Rails. By the end of this article, you’ll have a solid understanding of how to interact with a PostgreSQL database from your Ruby on Rails applications.
Prerequisites
- Ruby installed on your machine.
- Ruby on Rails installed on your machine.
- A running PostgreSQL server.
- 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 postgresqlcrudapp
This command will create a new Rails application named postgresqlcrudapp
. Navigate to the project directory using cd postgresqlcrudapp
.
Step 2: Configure PostgreSQL Connection
In your Rails application, you need to configure the connection to PostgreSQL. Open the config/database.yml
file and make sure it looks like this:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: your-database-host
port: 5432
database: your-database-name
username: your-username
password: your-password
Replace 'your-database-host'
, 'your-database-name'
, 'your-username'
, and 'your-password'
with your PostgreSQL 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 Product name:string price:decimal
This command generates a Product
model with name
and price
fields.
Next, apply the migration to create the products
table in your PostgreSQL database:
rails db:migrate
Step 4: Implement CRUD Operations
Create (C)
In your products_controller.rb
file, add the following code to create a new product:
def create
@product = Product.new(product_params)
if @product.save
redirect_to @product, notice: 'Product was successfully created.'
else
render :new
end
end
This code defines a create
action that creates a new product record in the products
table.
Read (R)
To retrieve and display product records, add the following code to your index
and show
actions:
def index
@products = Product.all
end
def show
@product = Product.find(params[:id])
end
These actions allow you to display a list of products and view individual product details.
Update (U)
For updating product records, add the following code to your edit
and update
actions:
def edit
@product = Product.find(params[:id])
end
def update
@product = Product.find(params[:id])
if @product.update(product_params)
redirect_to @product, notice: 'Product was successfully updated.'
else
render :edit
end
end
These actions allow you to edit and update product records.
Delete (D)
To delete product records, add the following code to your destroy
action:
def destroy
@product = Product.find(params[:id])
@product.destroy
redirect_to products_path, notice: 'Product was successfully deleted.'
end
This action deletes a product record.
Step 5: Create Views
Create views to display and interact with your products. In the app/views/products
folder, create the following files:
index.html.erb
: Display a list of products.show.html.erb
: Display an individual product.new.html.erb
: Create a new product.edit.html.erb
: Edit an existing product.
Here’s an example index.html.erb
file:
<h1>Products</h1>
<ul>
<% @products.each do |product| %>
<li>
<%= link_to product.name, product %>
</li>
<% end %>
</ul>
<%= link_to 'New Product', new_product_path %>
Step 6: Run Your Application
Start your Rails server by running:
rails server
Visit http://localhost:3000/products
in your web browser to interact with your CRUD application for PostgreSQL.
Conclusion
In this article, we’ve covered how to perform CRUD operations with PostgreSQL using Ruby on Rails. You’ve learned how to create a Rails application, configure the PostgreSQL connection, generate models, controllers, and views, and implement the CRUD operations for managing product records. PostgreSQL is a feature-rich and reliable database system, 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.
If you want to learn more about MySQL, please use our PostgreSQL tutorials and PostgreSQL Reference.