DEV Community

Samuel Lubliner
Samuel Lubliner

Posted on • Updated on

Interacting with Postgres from within a Rails app

config/database.yml

config/database.yml tells Rails which database to connect to.

Change the default rails_template_development to
database: my_contact_book.

rails dbconsole

Run rails dbconsole to launch psql and connect to the table specified in config/database.yml. \dt shows what tables are in the database.

Rails DB Gem

Start the web server with bin/dev. Navigate to the URL /rails/db for the database developer GUI.

ActiveRecord

A gem that automates the heavy lifting of connecting to a database and performing CRUD operations.

Models

For each table:

  1. Create a Ruby class to generate SQL
  2. Send it to the database
  3. Transform the data sent back into Ruby objects.

These classes are called “models” located in the app/models folder.

# app/models/contact.rb

class Contact
end
Enter fullscreen mode Exit fullscreen mode
  • Put a Ruby class in app/models.
  • Name the file the same as the class (snake_cased version). - Rails automatically requires that class in every other file in our app. This reduces require statements.
  • Name the class singularly. Each instance of this class represents one row from the contacts table.

Rails Console

  • In a new terminal tab, run rails console (rails c).
  • Rails console is similar to IRB, but it automatically requires all of the files and gems in the Rails app.
  • Instead of using irb, use rails c for a quick way of executing one line of Ruby at a time.

Inheriting from ActiveRecord::Base

# app/models/contact.rb

class Contact < ActiveRecord::Base
end
Enter fullscreen mode Exit fullscreen mode

The Contact class has inherited many methods that will help us CRUD data in the contacts table. Restart rails console to use the edits. Run Contact.new and Contact.count to explore.

Creating a new record

  • Create a new instance of the Contact class with Contact.new.
  • Assign that instance to a variable
  • ActiveRecord automatically adds attribute accessor methods for each column in the underlying table
  • Assign values to each column
  • Call .save to insert the row into the underlying table

Custom rake tasks

Store Ruby scripts in the lib/tasks folder

Within .rake files, use the task method.

The argument to task is a Symbol. Run the task from the bash prompt with rake <task name>.

Creating sample data

Write a task called “sample_contacts” to populate the contacts table.

Faker gem

Add a loop to our code to create a bunch of records all at once

The Faker gem includes a lot of methods for generating various kinds of values.

To “reset” the table by deleting all of the records before creating new, randomized records use the somewhat dangerous destroy_all method.

destroy_all will delete all of the records from a table. Be careful!

Retrieving existing records

Open a rails console session and work with our records using our model.

Attribute accessor methods

Top comments (0)