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.
rails new example_app
Resources:
rails generate model User name birthday:date
Resources:
rails db:migrate
Resources:
User.create(name: "Benito", birthday: Date.new(1987, 9, 21))
user = User.new(name: "Benito", birthday: Date.new(1987, 9, 21))
user.save
Resources:
User.find(id)
User.find_by(id: id)
User.find_by(name: "Benito")
User.all
User.where(name: "Benito")
Resources:
user = User.find(id)
user.update(name: "Benito")
user = User.find(id)
user.name = "Benito"
user.save
Resources:
user = User.find(id)
user.destroy
Resources:
Generate a migration to add the attribute…
rails generate migration add_country_to_users country
Resources:
User.order(:name)
User.order(name: :asc)
User.order(name: :desc)
Resources:
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
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ú
Update the Ramón’s birthday attribute to 1-mar-1981
using User#update
.
Update the María’s country attribute to Chile
using User#save
.
Update the country of all users to México
using User.update_all
.
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" }
]
Find all users from México using User.where
.
Find the user with name “Juana” and change her name to “Juanita”.
Add a new attribute to the users table called email
.
Update the email
attribute of all users with user@example.com
.
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
Find a user record from Colombia using find_by
and delete it.
Find all user records from Colombia using where
and delete them.
Create a system to admin all books in a library. You can call the the project
(rails app) my_library
.
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
The system should allow us to…
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
}
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
Returning the information as and string with the same format.
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)
Example.
Catalog.delete_book(book_id)
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
Returning the information as a string with the next format.
- J.K. Rowling
- Gabriel García Marquez
Returning the information as a string with the next format.
- 0439554934
- 9786070728792
Returning the information as a string with the next format.
- J.K. Rowling (7)
- Gabriel García Marquez (5)
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"
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.
I send an email each week, trying to share knowledge and fixes to common problems and struggles for ruby on rails developers, like How to fetch the latest-N-of-each record or How to test that an specific mail was sent or a Capybara cheatsheet. You can see more examples on Most recent posts or All post by topic.