Skip to content

Instantly share code, notes, and snippets.

View krokrob's full-sized avatar
🥞
Data Warehousing

Kevin ROBERT krokrob

🥞
Data Warehousing
View GitHub Profile
# Creating and Seeding ecommerce DB
import sqlite3
conn = sqlite3.connect('data/ecommerce.sqlite')
c = conn.cursor()
# Create customers table
c.execute("""
CREATE TABLE customers(
# List all customers (name + email), ordered alphabetically (no extra information)
SELECT first_name, last_name, email
FROM customers
ORDER BY last_name ASC;
# List tracks (Name + Composer) of the Classical playlist
SELECT t.name, t.composer
FROM tracks t
JOIN playlist_tracks pt ON t.id = pt.track_id
JOIN playlists p ON p.id = pt.playlist_id
WHERE p.name = 'Classical';
require_relative 'race'
# interface.rb
# Pseudo-code:
# 1. Print welcome
puts "🏇" * 10
puts "🏇 RAPIDO 🏇"
puts "🏇" * 10
# define horses names in an array
horses = [
def display_list(horses)
horses.each_with_index do |horse, index|
puts "#{index + 1} - #{horse}"
end
end
# display_list([
# 'Tempete du Desert',
# 'Jolly Jumper',
# 'Hermes',
# 'Hercules',
require "date"
def days_before_xmas(year = nil, month = nil, day = nil)
# set today and store it in a variable
if year.nil?
today = Date.today
year = today.year
else
today = Date.new(year, month, day)
end

Rails flow

Route

You want a route to the url /restaurants to display the list of your restaurants.

# config/routes.rb
resources :restaurants, only: [:index]
# Rake tasks
# db:create => create empty DB
# db:migrate => update DB schema (structure)
# db:seed => populate the DB (data)
# db:drop => !!!!! drop the DB (structure + data)
#
class Restaurant < ActiveRecord::Base
end
restaurant = Restaurant.new # => instance of restaurant
# Q8 write DB query with Ruby
# mapping between model and table
# mapping instance of model and a row in a table
# mapping instance variable and columns of a table
# ORM
# Q9 A ruby file which alter db schema structure (no data)
# ex: table, column
# Q10
class CreateAuthors < ActiveRecord::Migration[5.1]
def change
-- QUIZZ DB & ActiveRecord
-- Q1 A bunch of tables contining data connected between each other through a system of primary(id)/foreign keys(xxxxx_id)
-- Q2 1:N
-- N:N
-- 1:1
-- Q5 SQL
-- Q6
SELECT *
FROM books
WHERE publishing_year < 1985
require_relative 'patient'
class Room
class OverCapacityReachedError < RuntimeError; end
attr_reader :capacity
attr_accessor :id
def initialize(attributes = {})
@capacity = attributes[:capacity]