DEV Community

Kat
Kat

Posted on

Databases with Rails: Review

Here is a recap of some of the main points regarding databases and using databases with Ruby on Rails for reference compiled from the information from class.

Relational databases are databases structured around the data type "relation", which refers to a set of records modeled off a table. Relational databases use SQL. The apps that we build output SQL commands that create, read, update, or delete rows of data and send them to the database for processing.

To create a database and start working with it:

  1. Launch psql from a bash prompt
  2. In psql, create a database using the command CREATE DATABASE <database_name>.
  3. Connect to the database with the command \connect <database_name>.
  4. Create a table with the command
  CREATE TABLE <table_name> (
    ---table columns and datatype---
  );
Enter fullscreen mode Exit fullscreen mode
  1. Configure the Rails application to use the database by editing config/database.yml line 26 (current) to: database: <database_name>.
  2. Create a model called the singular form of the corresponding table name and inherit from ActiveRecord::Base.

To work with your database, use the terminal command rails console or rails c which launches psql and automatically connects to the database specified in config/database.yml.

ActiveRecord
ActiveRecord is a Ruby gem that automates most of the process of connecting to your database and performing CRUD operations.

Top comments (0)