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
良いアプリケーションは良いデータベースに宿る
Search
Ya-Suzuki
November 27, 2019
Technology
2
280
良いアプリケーションは良いデータベースに宿る
2019/11/27 Classi Rails Night
Ya-Suzuki
November 27, 2019
Tweet
Share
More Decks by Ya-Suzuki
See All by Ya-Suzuki
突撃!隣のフォームオブジェクト
y_s___3128
1
740
Other Decks in Technology
See All in Technology
エンジニアが一生困らない ドキュメント作成の基本
naohiro_nakata
2
140
Microsoft Intune アプリのトラブルシューティング
sophiakunii
1
400
RustとWebAssemblyを使って高速な画像処理をWebアプリで実行しよう
rebonire626
0
100
10分でわかるfreeeのQA
freee
1
3.5k
QAEチームが辿った3年 ボクらが改善業務にスクラムを選んだワケ / 20241108_cloudsign_ques23
bengo4com
0
590
AWS⼊社という選択肢、⾒えていますか
iwamot
2
1.1k
Forget efficiency – Become more productive without the stress
ufried
0
240
マルチモーダルデータ基盤の課題と観点
neonankiti
1
110
SREの前に
nwiizo
11
2.7k
LINEヤフー株式会社における音声言語情報処理AI研究開発@SP/SLP研究会 2024.10.22
lycorptech_jp
PRO
2
280
株式会社島津製作所_研究開発(集団協業と知的生産)の現場を支える、OSS知識基盤システムの導入
akahane92
1
180
軽量DDDはもういらない! スタイルガイド本で OOPの実装パターンを学ぼう
panda_program
29
11k
Featured
See All Featured
Six Lessons from altMBA
skipperchong
26
3.5k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
YesSQL, Process and Tooling at Scale
rocio
168
14k
For a Future-Friendly Web
brad_frost
175
9.4k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
What's new in Ruby 2.0
geeforr
343
31k
Building Adaptive Systems
keathley
38
2.3k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
505
140k
4 Signs Your Business is Dying
shpigford
180
21k
Into the Great Unknown - MozCon
thekraken
32
1.5k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Transcript
ྑ͍ΞϓϦέʔγϣϯ ྑ͍σʔλϕʔεʹ॓Δ 2019.11.27 Classi Rails Night ླ߁म(@y_s____731)
• ླ߁म • AGBIOTECHגࣜձࣾ • Ruby/Rails/ΤϯδχΞྺ 3 • લ৬T̋Y̋TAͰઃඋͷमཧͯ͠·ͨ͠ɻ •
twitter @y_s_____731
લஔ͖
σʔλϕʔεɾɾɾ • ʑ͞ΘͬͯΔͣ • ҙࣝͤͣͱ͞ΘΕͯ͠·͏ • RailsʢActiveRecordʣͷੌ͞
ͳ͔ͳ͔ݴٴ͞Εͳ͍
͍͟ຊ൪ڥˠແࣄࢮ
σʔλϕʔεΛΒͣʹ ྑ͍ΞϓϦέʔγϣϯ࡞Εͳ͍
None
Classi͞ΜͷΠϕϯτ͕ ݸਓతʹλʔχϯάϙΠϯτ
1.໊લॏཁ
ͦΜͳ͜ͱ͋ͬͯɺࢲͷઃܭ্ͷ࠲ӈͷʮ໊લॏཁʯ Ͱ͢ɻ ͋ΒΏΔػೳΛσβΠϯ͢Δ࣌ʹɺࢲͦͷ໊લʹͬͱ ͩ͜ΘΓ·͢ɻ ϓϩάϥϚͱͯ͠ͷΩϟϦΞͷதͰɺదͳ໊લΛ͚ͭΔ͜ ͱ͕Ͱ͖ͨػೳޭ͠ɺͦ͏Ͱͳ͍ػೳʹ͍ͭͯޙͰޙ չ͢Δ͜ͱ͕ଟ͔ͬͨΑ͏ʹࢥ͏͔ΒͰ͢ɻ by Matz
σʔλϕʔεಉ͡
σʔλϕʔεʹ͓͚Δ໊લɾɾɾ • ςʔϒϧ໊ • ΧϥϜ໊
୭͕Ͳ͏ΈͯɺԿΛදͯ͠Δ͔͔Δ
ந× ۩ମత̋ ʢUser → Teature, Studentͱ͔ʣ நతͳ໊લ͕ᐆດʹͳΓ͕ͪ ↪︎FatModelʹͳΓ͍͢
ུ͞ͳ͍ ʢྫ.༵ dow× day_of_week̋ʣ ಛʹαʔϏεݻ༗ͷݴ༿ͳͲུ͞ΕΔͱ ޙ͔Βೖͬͨਓ͖ͬͱਏ͍
2.੍Λ׆༻͢Δ
੍ RDBMS͕ඪ४Ͱ͍࣋ͬͯΔ ೖྗΛ੍ݶ͢Δػೳ
੍ͷछྨɾɾɾ • NOT NULL੍ • ForeignKey(֎෦Ωʔ)੍ • UNIQUE੍ • CHECK੍…etc
NOT NULL ੍ NULLΛېࢭ͢Δ
class CreateArticles < ActiveRecord::Migration[5.2] def change create_table :articles do |t|
t.string :title, null: false t.text :content, null: false t.timestamps end end end
࠷ॳʹ͚ͭΔͷ؆୯ɻ Ͱɺޙ͔ΒՃ͠ਏ͍͜ͱɻ
جຊతʹNOT NULL͚ͭΔ ↓ Կ͕NULLABLEͳͷ͔Λߟ͑Δ
֎෦Ωʔ ੍ ผͷςʔϒϧͷಛఆͷΧϥϜʹ͋Δ͔͠ ೖྗͰ͖ͳ͍Α͏ʹ͢Δػೳ
Α͘͏ͷதؒςʔϒϧ ϑΥϩʔɾϑΥϩϫʔΈ͍ͨͳؔ࿈͚Ͱ ଘࡏ͠ͳ͍ϢʔβʔΛϑΥϩʔͰ͖ͳ͍Α͏ʹ͢Δ
None
class CreateRelationships < ActiveRecord::Migration[5.2] def change create_table :relationships do |t|
t.references :user, foreign_key: true, null: false t.references :followed, foreign_key: { to_table: :users }, null: false t.timestamps end end end
ΧϥϜ໊ͱࢀরઌͷ໊લ͕ಉ͡ foreign_key: true ҧ͏߹ foreign_key: { to_table: :ςʔϒϧ໊ }
DBͷ߹ੑΛอͭ࠷ޙͷࡆ = ੍
੍ͪΌΜͱ͍·͠ΐ͏
3.ΞϓϦέʔγϣϯ͋Γ͖ͰDBΛߟ͑ͳ͍
Railsͷಛ ςʔϒϧ = Ϋϥε
ςʔϒϧઃܭͱ࣮Λಉ࣌ʹߟ͕͑ͪ
ҰͭͷΞΧϯτʹෳͷϩʔϧ͕͋Δ ྫ.ചΓखͱങ͍ख
None
class User < ApplicationRecord # ڞ௨ͷใ end class Seller <
ApplicationRecord has_many :selling_items, dependent: :destroy ɹ# ചΓखʹؔ͢ΔϦϨʔγϣϯ # ചΓखʹؔ͢ΔϏδωεϩδοΫ end class Buyer < ApplicationRecord # ങ͍खʹؔ͢ΔϦϨʔγϣϯ # ങ͍खʹؔ͢ΔϏδωεϩδοΫ end
త ϦϨʔγϣϯͱϏδωεϩδοΫͷׂ ↓ ςʔϒϧ࡞Δͷ
None
class User < ApplicationRecord # ڞ௨ͷใ end class Seller <
User has_many :selling_items, dependent: :destroy ɹ# ചΓखʹؔ͢ΔϦϨʔγϣϯ # ചΓखʹؔ͢ΔϏδωεϩδοΫ end class Buyer < User # ങ͍खʹؔ͢ΔϦϨʔγϣϯ # ങ͍खʹؔ͢ΔϏδωεϩδοΫ end
Ϋϥεͱͯ͠ඞཁ ≠ ςʔϒϧ࡞Δ
ݴΘΕΔͱવͳΜ͚ͩͲ ҰਓͰͬͯΔͱҊ֎ϛε͍ͬͯΔ
·ͱΊ
·ͱΊ • ΞϓϦέʔγϣϯͷϑϨʔϜϫʔΫ͕มΘΔ͜ͱ͋ͬͯ σʔλϕʔε͕·ΔͬͱมΘΔ͜ͱͳ͔ͳ͔ͳ͍ɻ • ͔ͩΒͦ͜ɺσʔλϕʔεઃܭͳͲΛ͓Ζ͔ͦʹͤͣɺͪΌ ΜͱΔඞཁ͕͋Δɻ • ͨͩɺઃܭҰਓͰͬͯΔͱ͍ͭͷؒʹ͔ϋϚͬͯͨΓਏ͍ ͷͰɺڧ͍ਓͱҰॹʹͱ͔ɺଟਓͰͬͨ΄͏͕Αͦ͞͏ɻ