49 Days of Ruby: Day 31 - Working with Databases

Ben Greenberg - Apr 27 '21 - - Dev Community

Welcome to day 31 of the 49 Days of Ruby! 🎉

Databases are a really big subject. Today, we are going to take a few steps into the ocean of databases. We'll spend a moment explaining what they are, and offering a tip or two on working with them in Ruby.

What is a Database?

At its most fundamental level, a database is a piece of software that stores information. That information is stored in tables, and each table has rows and columns of data.

SQL Query

Database Example from Wikipedia

Databases are incredibly important if you want your application to keep information in between uses. Let's say you build a website with member profiles. Should a member re-create their profile each time they log in? Probably not. You would store their data in a member database.

Databases in Ruby

There are different types of databases with different languages used to communicate to them. The most common you will encounter with Ruby applications are SQL databases.

SQL is a language for writing database queries. It is how you ask the database for information, add info to the database, and generally move around your data.

Within SQL-type databases, there are lots of different options. In this example, we'll show how to get started with PostgreSQL, a prevalent database type.

The ruby-pg gem provides an interface between your Ruby code and your PostgreSQL database.

You would require the gem like any other gem in your code:

require 'pg'
Enter fullscreen mode Exit fullscreen mode

Then, you establish a connection your PostgreSQL database providing the database name and the database username:

connection = PG.connect :dbname => 'my_database', :user => 'database_user'
Enter fullscreen mode Exit fullscreen mode

Now you have an instantiated connection to your database, you can start writing SQL queries to it!

W3Schools has an intro to SQL that may be helpful to read.

Let's say you want to retrieve all your coffee records:

results = connectio.exec "SELECT * FROM Coffees"
Enter fullscreen mode Exit fullscreen mode

The result will be all the coffee records in the results variable. You can then output it using an iterator:

results.each do |row|
  puts "#{row['coffee_name]}: #{coffee['caffeine_strength']}"
end
Enter fullscreen mode Exit fullscreen mode

I made up those column names of coffee_name and caffeine_strength for the example. Your database will be whatever you specify.

There is so much more to learn about databases. Share what you discover today with the community!

Come back tomorrow for the next installment of 49 Days of Ruby! You can join the conversation on Twitter with the hashtag #49daysofruby.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .