Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Intro to Databases (Rails Girls PHL)
Search
Ernesto Tagwerker
December 03, 2016
Programming
0
63
Intro to Databases (Rails Girls PHL)
A quick intro to databases for Rails Girls PHL.
Ernesto Tagwerker
December 03, 2016
Tweet
Share
More Decks by Ernesto Tagwerker
See All by Ernesto Tagwerker
Stuck in the Tar Pit at Sin City Ruby '24
etagwerker
0
89
Lightning Talk: Escaping the Tar Pit
etagwerker
0
71
Fortify Rails Webinar
etagwerker
0
1.8k
Here Be Dragons: The Hidden Gems of Technical Debt
etagwerker
0
130
Lessons Learned from Open Source
etagwerker
0
56
Upgrading Rails: The Dual-Boot Way
etagwerker
1
440
Ruby 3.0 & Rails 6.1
etagwerker
0
160
RubyMem: The Leaky Gems Database for Bundler at Ruby Kaigi Takeout 2020
etagwerker
0
110
Escaping The Tar Pit at NYC.rb
etagwerker
0
68
Other Decks in Programming
See All in Programming
Git Rebase
bkuhlmann
11
1.6k
PostmanでAPIの動作確認が楽になった話
h455h1
0
160
AWS Application Composerで始める、 サーバーレスなデータ基盤構築 / 20240406-jawsug-hokuriku-shinkansen
kasacchiful
1
260
単体テストを書かない技術 #phpcon_odawara
o0h
PRO
26
8.1k
PHPの次期バージョンはこの時期どうなっているのか - Internalsの開発体制について - PHPカンファレンス小田原
youkidearitai
PRO
1
190
効率化に挑戦してみたらモバイル開発が少し快適になった話
ryunakayama
0
120
Front-end application development, Symfony-style(s)
dunglas
2
1.9k
Git Lint
bkuhlmann
4
750
R言語の環境構築と基礎 Tokyo.R 112
bob3bob3
0
250
TYPO3 v13 – The road to LTS: What's new and new APIs
luisasofie_xoxo
0
190
Tailwind CSSを本気でカスタマイズする方法
fsubal
13
5k
0→1と1→10の狭間で Javaという技術選定を振り返る/Reflecting on the Decision to Choose Java Between Scaling from 0 to 1 and 1 to 10
jaguar_imo
2
380
Featured
See All Featured
The Cult of Friendly URLs
andyhume
74
5.7k
Building Effective Engineering Teams - LeadDev
addyosmani
27
1.8k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
104
6.6k
StorybookのUI Testing Handbookを読んだ
zakiyama
11
4.6k
Scaling GitHub
holman
457
140k
5 minutes of I Can Smell Your CMS
philhawksworth
199
19k
The Language of Interfaces
destraynor
151
23k
Intergalactic Javascript Robots from Outer Space
tanoku
266
26k
Building a Modern Day E-commerce SEO Strategy
aleyda
16
6.4k
Designing for humans not robots
tammielis
247
25k
Debugging Ruby Performance
tmm1
70
11k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
124
32k
Transcript
Intro to Databases Rails Bridge Philadelphia December 2016
Ernesto Tagwerker @etagwerker
Spreadsheets
None
From spreadsheets to databases
None
None
None
Primary Keys
Primary Keys
None
Relational Databases
Relations (Tables)
Relational Database Management Systems (DBMS)
MySQL Postgres SQLite* MS SQL Server and more … DBMS
Alternatives
Databases in Rails
$ rails new --help # database # (options: mysql/oracle/postgresql/sqlite3/ frontbase/ibm_db/sqlserver/jdbcmysql/
jdbcsqlite3/jdbcpostgresql/jdbc) $ rails new polls —database=postgresql $ rails new polls # defaults to sqlite3
Where is the database?
SQL (for data) • Create • Retrieve • Update •
Delete
Rails Migrations # db/migrate/20161124163416_create_topics.rb class CreateTopics < ActiveRecord::Migration def change
create_table :topics do |t| t.string :title t.text :description t.timestamps null: false end end end
Rails Migrations $ rake db:migrate == 20161124163416 CreateTopics: migrating =====================================
-- create_table(:topics) -> 0.0496s == 20161124163416 CreateTopics: migrated (0.0497s) ============================
Rails Migrations CREATE TABLE `topics` ( `id` int(11) NOT NULL
AUTO_INCREMENT, `title` varchar(255) DEFAULT NULL, `description` text, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL, PRIMARY KEY (`id`) )
SQL (for table definitions) • Create table • Modify table
• Drop table
Models # app/models/vote.rb class Vote < ActiveRecord::Base belongs_to :user belongs_to
:topic end # app/models/topic.rb class Topic < ActiveRecord::Base has_many :votes end # app/models/user.rb class User < ActiveRecord::Base has_many :votes end
Models (create) $ rails console > Topic.create(title: 'Intro to Database',
description: 'This is awesome!') SQL (1.4ms) INSERT INTO `topics` (`title`, `description`, `created_at`, `updated_at`) VALUES ('Intro to Database', 'This is awesome!', '2016-11-24 16:57:13', '2016-11-24 16:57:13') => #<Topic id: 1, title: "Intro to Database", description: "This is awesome!", created_at: "2016-11-24 16:57:13", updated_at: "2016-11-24 16:57:13">
Models (retrieve) > topic = Topic.find 1 Topic Load (1.6ms)
SELECT `topics`.* FROM `topics` WHERE `topics`.`id` = 1 LIMIT 1 => #<Topic id: 1, title: "Intro to Database", description: "This is awesome!", created_at: "2016-11-24 16:57:13", updated_at: "2016-11-24 16:57:13">
None
Models (update) > topic.title = "Intro to Databases" => "Intro
to Databases" > topic.save SQL (2.3ms) UPDATE `topics` SET `title` = 'Intro to Databases', `updated_at` = '2016-11-24 17:00:47' WHERE `topics`.`id` = 1 => true
Models (destroy) > topic.delete SQL (10.0ms) DELETE FROM `topics` WHERE
`topics`.`id` = 1 => #<Topic id: 1, title: "Intro to Databases", description: "This is awesome!", created_at: "2016-11-24 16:57:13", updated_at: "2016-11-24 17:00:47">
Models (count) > Topic.count (2.1ms) SELECT COUNT(*) FROM `topics` =>
0
How to use models
Topic.find 123 Topic.first Topic.last Topic.count To interact with tables
topic = Topic.find 123 topic.title = “New title” topic.save To
modify records
Thank you!
Questions?