Skip to content

Instantly share code, notes, and snippets.

@caioertai
Created October 26, 2020 22:49
Show Gist options
  • Save caioertai/b27e40baa516110691f0de9af0e9fe71 to your computer and use it in GitHub Desktop.
Save caioertai/b27e40baa516110691f0de9af0e9fe71 to your computer and use it in GitHub Desktop.
class Flat
def initialize(attrs = {})
# DATA
# @id
# @name
# @price_per_night
# ....
end
### ALL
# Tell DB to get results as hash
# Ask DB for all flats
# ---
# E.g.:
# id name price description
# ------ ----------- ----- -------------------------------
# 1 Casa Rosada 50 Great flat at the top of a hill
# 2 Studio in C 30 Small and cozy studio in Cuerna
# 3 Flat in Cod 30 Comfortable flat in Condesa
# ---
# => [{ id: 1, name: "Casa Rosada"... }, {...}]
# Use the info from the DB
# to instantiate flats
### FIND
# DB.results_as_hash = true
# DB.execute("SELECT * FROM flats WHERE id = ??")
# { id: 1, name: "Casa Rosada"... }
# Instantiate and return one
### DESTROY
# DELETE FROM flats
# WHERE id = ????
### CREATE
# ?
### UPDATE
# ?
end
### An example of how a list controller would look like
## List all flats
# Ask model for all flats
# Ask view to display them
-- Create table
CREATE TABLE `flats` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`name` TEXT,
`price_per_night` INTEGER,
`description` TEXT
);
-- Read
---- Just one flat
-- https://www.airbnb.com/rooms/45884971
SELECT *
FROM flats
WHERE id = 45884971
---- All flats
SELECT *
FROM flats
-- Create
---- Create a flat
INSERT INTO flats (name, price_per_night, description)
VALUES ('Flat at Condesa', 30, 'Comfortable flat in Condesa')
-- Update
---- Changes one record with id = n
UPDATE flats
SET name = 'Casa Rosada', price_per_night = 50
WHERE id = 1
---- Caution!! Changes ALL RECORDS
UPDATE flats
SET name = 'Casa Morada'
-- Destroy
---- Deletes record with id 32
DELETE FROM flats
WHERE id = 32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment