Benito Serna
Trying to build software that works

Intro to Active Record - The CRUD superpowers

Did you complete a Ruby on Rails tutorial, but want a more focused practice on Active Record?

Do you know other languages and frameworks and want to learn the basics of the magic on Active Record?

If this is your case I think this guide can help you =)

The guide has three sections, a quick referece for questions like how to run a migration?, how to create a model?, etc. A section with some excercises to execute on the rails console. And third section with a small project to check you knowledge of the basics of Active Record.

Index

Reference

How to create a rails app?

rails new example_app

Resources:

How to create a model using the rails generator?

rails generate model User name birthday:date

Resources:

How to run a migration?

rails db:migrate

Resources:

How to create a record in the database?

User.create(name: "Benito", birthday: Date.new(1987, 9, 21))

user = User.new(name: "Benito", birthday: Date.new(1987, 9, 21))
user.save

Resources:

How to find records in the database?

User.find(id)
User.find_by(id: id)
User.find_by(name: "Benito")
User.all
User.where(name: "Benito")

Resources:

How to update a record in the database?

user = User.find(id)
user.update(name: "Benito")

user = User.find(id)
user.name = "Benito"
user.save

Resources:

How to delete a record in the database?

user = User.find(id)
user.destroy

Resources:

How to add a new attribute to a model?

Generate a migration to add the attribute…

rails generate migration add_country_to_users country

Resources:

How to sort a list of records?

User.order(:name)
User.order(name: :asc)
User.order(name: :desc)

Resources:

Console exercises

Excercise 1

Create 2 different users using User.new and User#save

nombre = María, fecha = 10-sep-2000, país = México
nombre = Pedro, fecha = 9-may-1990, país = Colombia

Excercise 2

Create 2 different users using User.create

nombre = Ramón, fecha = 1-mar-1980, país = México
nombre = Florinda, fecha = 9-feb-2003, país = Perú

Excercise 3

Update the Ramón’s birthday attribute to 1-mar-1981 using User#update.

Excercise 4

Update the María’s country attribute to Chile using User#save.

Excercise 5

Update the country of all users to México using User.update_all.

Excercise 6

Create users using the next array of hashes

[
  { name: "Jose", birthday: Date.new(2000, 10, 10), country: "México" },
  { name: "Juan", birthday: Date.new(1987, 7, 3), country: "Colombia" },
  { name: "Ricardo", birthday: Date.new(1999, 5, 4), country: "Colombia" },
  { name: "Juana", birthday: Date.new(2001, 4, 9), country: "México" },
  { name: "Josefa", birthday: Date.new(1993, 10, 8), country: "México" }
]

Excercise 7

Find all users from México using User.where.

Excercise 8

Find the user with name “Juana” and change her name to “Juanita”.

Excercise 9

Add a new attribute to the users table called email.

Excercise 10

Update the email attribute of all users with user@example.com.

Excercise 11

Update the email of all users with:

"#{user.name}@example.com"

# For example...
# Jose@example.com
# Juanita@example.com
# ... given the name of each user

Excercise 12

Find a user record from Colombia using find_by and delete it.

Excercise 13

Find all user records from Colombia using where and delete them.

Project

Create a system to admin all books in a library. You can call the the project (rails app) my_library.

Instructions

We are not going to use the controllers or views. So, we are going to “expose” the system using a module Catalog that we are going to put in the app/models folder and we are going to test the functions using the rails console.

For example:

module Catalog
  def self.register_book(attrs)
  end
end

Features

The system should allow us to…

1. Register books

With “Name”, “Author’s name”, “ISBN”, “Publication date” y “Copies count”

Example:

attrs = {
  name: "Cien Años de Soledad",
  author_name: "Gabriel García Marquez",
  isbn: "12341234",
  published_on: Date.new(2000, 10, 20),
  copies_count: 30
}

Catalog.register_book(attrs)

Here are some books that you can use as example (this is not real data):

{
  name: "The Outsider",
  author_name: "Stephen King",
  isbn: "12341234",
  published_on: Date.new(2007, 10, 20),
  copies_count: 30,
}

{
  name: "The President Is Missing",
  author_name: "Bill Clinton",
  isbn: "87623464",
  published_on: Date.new(2015, 10, 20),
  copies_count: 10
}

{
  name: "Harry Potter and the Sorcerer's Stone",
  author_name: "J.K. Rowling",
  isbn: "876212344",
  published_on: Date.new(2016, 2, 20),
  copies_count: 10
}

{
  name: "Something in the Water",
  author_name: "Catherine Steadman",
  isbn: "876212341234",
  published_on: Date.new(2017, 2, 20),
  copies_count: 30
}

{
  name: "Harry Potter and the Goblet of Fire",
  author_name: "J.K. Rowling",
  isbn: "543876212344",
  published_on: Date.new(2015, 2, 20),
  copies_count: 20
}

{
  name: "Little Fires Everywhere",
  author_name: "Celeste NG",
  isbn: "5438762109874",
  published_on: Date.new(2016, 2, 20),
  copies_count: 12
}

2. Find a book with the registration id

Returning the information as an string with the next format:

ID: 1234
ISBN: 9786070728792
Name: Cien Años de Soledad
Author: Gabriel García Marquez
Publication year: 2000
Copies count: 5

3. Find a book by ISBN

Returning the information as and string with the same format.

4. Update the attributes of a book

Giving the id of the book and the attributes that we want to update.

Example.

attrs = {
  name: "Cien Años de Soledad",
  author_name: "Gabriel García Marquez"
  # ...
}

Catalog.update_book(book_id, attrs)

5. Delete a book with its id.

Example.

Catalog.delete_book(book_id)

6. Return a list with all the books.

Returning the information as a string with the next format:

ID: 123 - (0439554934) Harry Potter and the Philosopher's Stone, by J.K. Rowling - Year: 2000
ID: 324 - (9786070728792) Cien Años de Soledad, by Gabriel García Marquez - Year: 1999

7. Return a list of books for different kind of requests…

  1. All books sorted by year of publication in an ascendent way.
  2. All books sorted by year of publication in a descendent way.
  3. All books published in a given year.
  4. All books from a given author.

8. Return a list with the names of all authors from the books that we have.

Returning the information as a string with the next format.

- J.K. Rowling
- Gabriel García Marquez

9. Returning a list with all ISBN that we have sorted in an ascendent way.

Returning the information as a string with the next format.

- 0439554934
- 9786070728792

10. Returning a list with all the names of the authors and the number of books (no copies) that we have of them.

Returning the information as a string with the next format.

- J.K. Rowling (7)
- Gabriel García Marquez (5)

11. Validate the presence of the name of the book at registration

If someone tries to register a book without name, the system will return an error like this…

Example:

attrs = {}
Catalog.register_book(attrs)
=> "Error: name can't be blank"

12. Validate that when the book is updated the name could not be deleted.

Example:

attrs = {author_name: "Mario Benedetti"}
Catalog.update_book(book_id, attrs)
# Here, only the author name should be updated.

attrs = {name: nil, author_name: "Mario Benedetti"}
Catalog.update_book(book_id, attrs)
# Here, also only the author name should be updated.

Are you also trying to write software that works?

Maybe you can join the newsletter and we can try together.