Created
May 1, 2018 17:37
-
-
Save barangerbenjamin/c0133c093aaef959bcc934a2a29d4cc4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Q1 - What's a relational database? | |
a set of tables linked to each other thanks to a system of primary / foreign keys | |
# Q2 - What are the different "table relationships" you know? | |
1:n, 1 to many | |
n:n, many to many | |
1:1, one to one | |
# Q3 - Consider this e-library service. An author, defined by | |
# his name have several books. | |
# A book, defined by its title and publishing year, | |
# has one author only. What's this simple database scheme. | |
# Please draw it! | |
# Q4 - A user, defined by his email, can read several books. | |
# A book (e-book!!) can be read by several user. | |
# We also want to keep track of reading dates. | |
# Improve your e-library DB scheme with relevant tables | |
# and relationships. | |
# Q5 - What's the language to make queries to a database? | |
Structured Query Language => SQL | |
# Q6 - What's the simple query to get books written before 1985? | |
SELECT * FROM books | |
WHERE publishing_year < 1985 | |
# Q7 - What's the simple query to get the 3 most recent books | |
# written by Jules Verne? | |
SELECT * FROM books b | |
JOIN authors a ON a.id = b.author_id | |
WHERE a.name = "Jules Verne" | |
ORDER BY b.publishing_year DESC | |
LIMIT 3; | |
# Q8 - What's the purpose of ActiveRecord? | |
Active Record is a design pattern that maps your objects to a relational database | |
This pattern is knows as ORM (Object Relational Mapping) | |
and its also a gem | |
# Q9 - What's a migration? How do you run a migration? | |
A script that modifies the database schema | |
rake db:migrate | |
# Q10 - Complete migrations to create your e-library database | |
class CreateAuthors < ActiveRecord::Migration[5.1] | |
def change | |
create_table :authors do |t| | |
t.string :name | |
t.timestamps null: false | |
end | |
end | |
end | |
class CreateBooks < ActiveRecord::Migration[5.1] | |
def change | |
create_table :books do |t| | |
t.string :title | |
t.integer :publishing_year | |
t.references :author, index: true | |
t.timestamps null: false | |
end | |
end | |
end | |
class CreateUsers < ActiveRecord::Migration[5.1] | |
def change | |
create_table :users do |t| | |
t.string :email | |
t.timestamps null: false | |
end | |
end | |
end | |
class CreateReadings < ActiveRecord::Migration[5.1] | |
def change | |
create_table :readings do |t| | |
t.date :date | |
t.references :book, index: true | |
t.references :user, index: true | |
t.timestamps null: false | |
end | |
end | |
end | |
# Q11 - Write a migration to add a category column to the books | |
# table. | |
class AddCategoryToBooks < ActiveRecord::Migration[5.1] | |
def change | |
add_column :books, :category, :string | |
end | |
end | |
# Q12 - Define an ActiveRecord model for each table of your DB. | |
# Add the ActiveRecord associations between models. | |
class Author < ActiveRecord::Base | |
has_many :books | |
end | |
class Book < ActiveRecord::Base | |
has_many :readings | |
belongs_to :author | |
has_many :users, through: :readings | |
end | |
class Reading < ActiveRecord::Base | |
belongs_to :user | |
belongs_to :book | |
end | |
class User < ActiveRecord::Base | |
has_many :readings | |
has_many :books, through: :readings | |
end | |
# Q13 - Complete the following code using the relevant | |
# ActiveRecord methods. | |
# 1. Add your favorite author to the DB | |
Author.create(name: "someone") | |
# 2. Get all authors | |
Author.all | |
# 3. Get author with id=8 | |
Author.find(8) | |
# 4. Get author with name="Jules Verne", store it in a | |
# variable: jules | |
jules = Author.find_by(name: "Jules Verne") | |
# 5. Get Jules Verne's books | |
jules.books | |
# 6. Create a new book "20000 Leagues under the Seas", | |
# it's missing in DB. | |
# Store it in a variable: twenty_thousand | |
twenty_thousand = Book.new(title: "20000 Leagues under the Seas") | |
# 7. Add Jules Verne as this book's author | |
twenty_thousand.author = jules | |
# 8. Now save this book in the DB! | |
twenty_thousand.save | |
# The end | |
# Q14 - Add validations of your choice to the Author class. | |
# Can we save an object in DB if its validations do not pass? NO | |
class Author < ActiveRecord::Base | |
has_many :books | |
validates :name, presence: true | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment