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
どうしてこうなった?から理解するActive Recordの関連の裏側
Search
Shinichi Maeshima
August 24, 2024
Programming
5
710
どうしてこうなった?から理解するActive Recordの関連の裏側
大阪Ruby会議04
https://rubykansai.github.io/osaka04/
での発表内容です。
Shinichi Maeshima
August 24, 2024
Tweet
Share
More Decks by Shinichi Maeshima
See All by Shinichi Maeshima
Exceptional Rails
willnet
6
6.7k
Breaking the Flaky Test Cycle
willnet
2
1.9k
mrskで広がるインフラの選択肢
willnet
1
960
アプリケーションを長期にわたって無理なく運用するためのたったひとつの方法
willnet
2
2k
HotwireからDHHが考えるこれからのRailsとJSの付き合い方を知る
willnet
14
13k
Rails6.1で新しく入る機能について
willnet
12
15k
Concerns about Concerns
willnet
11
33k
Clean Test Code Revised
willnet
34
18k
個人でつくるwebサービス
willnet
13
9.8k
Other Decks in Programming
See All in Programming
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
9
1.3k
5年分のツケを一気に払った話
soogie
3
1.3k
Pythonによるイベントソーシングへの挑戦と現状に対する考察 / Challenging Event Sourcing with Python and Reflections on the Current State
nrslib
3
1.2k
ROS 2のZenoh対応とZenohのROS 2対応
takasehideki
2
300
タイミーにおけるデータの利用シーンと データ基盤の挑戦
marufeuille
4
3.2k
標準ライブラリの動向とイテレータのパフォーマンス
makki_d
3
200
ML-прайсинг_на_Lamoda__вошли_и_вышли__приключение_на_20_минут__Слава_Цыганков.pdf
lamodatech
0
160
モジュラモノリス、その前に / Modular monolith, before that
euglena1215
6
690
Compose Multiplatform과 Ktor로 플랫폼의 경계를 넘어보자
kwakeuijin
0
270
文化が生産性を作る
jimpei
3
560
선언형 UI를 학습할 때 알아둬야하는 키워드들
l2hyunwoo
0
140
UnJSで簡単に始めるCLIツール開発 / cli-tool-development-with-unjs
aoseyuu
2
290
Featured
See All Featured
Documentation Writing (for coders)
carmenintech
65
4.4k
Creatively Recalculating Your Daily Design Routine
revolveconf
217
12k
Pencils Down: Stop Designing & Start Developing
hursman
119
11k
Visualization
eitanlees
143
15k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Scaling GitHub
holman
458
140k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
BBQ
matthewcrist
85
9.2k
Learning to Love Humans: Emotional Interface Design
aarron
272
40k
RailsConf 2023
tenderlove
28
840
The Pragmatic Product Professional
lauravandoore
31
6.2k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
43
6.5k
Transcript
Osaka RubyKaigi 04 @willnet Shinichi Maeshima Ͳ͏ͯ͜͠͏ͳͬͨʁ ͔Βཧղ͢Δ Active Recordͷཪଆ
Shinichi Maeshima @netwillnet @willnet https://blog.willnet.in Willnet Inc.
ੲ͚ͩେࡕʹॅΜͰ͍·ͨ͠
HJO[BSCͬͯ·͢
ٕज़ސۀΛ͍ͯ͠·͢
͓ࣄ4/4తͳͷΛ։ൃ ͍ͯ͠·͢
8F`SFOPUIJSJOH
ސઌʹ͍ͭͯ؇ื
େࡕ3VCZձٞʹ ొஃ͢Δͷճ
None
None
աڈ࡞ͷࢿྉ 4QFBLFS%FDLϒϩάͰ ֬ೝͰ͖·͢
͔͜͜Βຊ
Osaka RubyKaigi 04 @willnet Ͳ͏ͯ͜͠͏ͳͬͨʁ ͔Βཧղ͢Δ Active Recordͷཪଆ
Osaka RubyKaigi 04 @willnet Ͳ͏ͯ͜͠͏ͳͬͨʁ ͔Βཧղ͢Δ Active Recordͷཪଆ
Active Record is like a magic w ΈΜͳେ͖"DUJWF3FDPSE w نʹΑΓɺ·ΔͰຐ๏ͷΑ͏ʹಈ͘
w ͜Μͳײ͡Ͱಈ͔͘ͳʁͱࢥͬͨΒ͍͍ͩͨͦͷΑ͏ʹಈ͘ w ͕ɺۃكʹͦ͏Ͱͳ͍͜ͱ͋Δ
None
None
None
None
ؔ࿈Λఆٛ͢Δͱಈతʹϝιου ͕ੜ͞ΕΔ
ίʔϧόοΫੜ͞Ε͍ͯΔ
ʮϞσϧอଘ࣌ʹؔ࿈ઌอଘ͢Δʯ Λ͢ΔίʔϧόοΫ
͜ΕΛ࣮ߦͨ͠ͱ͖ͷڍಈΛઆ໌͠·͢
BVUPTBWF@BTTPDJBUFE@SFDPSET @GPS@VTFSͷ࣮ࡍͷॲཧ
͍Ζ͍Ζলུͨ͠ίʔυ
ओʹBVUPTBWFUSVF DPNQPTJUF@QSJNBSZ@LFZTͳͲΛলུ͍ͯ͠·͢
ؔ࿈ͷϝλใ
CFMPOHT@UPVTFSͳͷͰVTFS͕ฦͬͯ͘Δ lVTFS@JEz͕ฦͬͯ͘Δ
ϞσϧΦϒδΣΫτຖʹੜ͞ΕΔؔ࿈ΦϒδΣΫτɻ 1PTUVTFSͳͲͷؔ࿈ϝιου શ෦͜ͷΦϒδΣΫτܦ༝Ͱ࣮ߦ͞ΕΔɻ ؔ࿈ઌͷΦϒδΣΫτ࣋ͭɻ
ؔ࿈ઌͷΦϒδΣΫτ͕ඥ͍ͮͯͳ͔ͬͨΒ ͳʹ͠ͳ͍
ؔ࿈ઌͷΦϒδΣΫτ͕ ӬଓԽ͞Ε͍ͯͳ͔ͬͨΒTBWF
ؔ࿈ઌͷQSJNBSZ@LFZΛ ࣗͷ֎෦Ωʔͱͯ͠ઃఆ͢Δ
ίʔϧόοΫ࣮ߦޙʹΫΤϦ͕ൃߦ ͞ΕΔ Post#save before_save User#save INSERT INTO users … post.user_id
= user.id INSERT INTO posts …
IBT@NBOZେ·͔ͳΈ ಉ͡
λΠϛϯάҧ͏
5*14ίʔϧόοΫͷൃՐॱ൪ BGUFS@ DSFBUFcVQEBUF BGUFS@TBWFͷॱ
͜ΕΛ࣮ߦͨ͠ͱ͖ͷڍಈΛઆ໌͠·͢
BVUPTBWF@BTTPDJBUFE@SFDPSET @GPS@QPTUTͷ࣮ࡍͷॲཧ
͍Ζ͍Ζলུͨ͠ίʔυ
ϩʔυࡁΈͷؔ࿈ઌͷ͏ͪɺ݅ʹϚον͢ΔͷΛऔಘ͢Δ
আࡁΈͷΦϒδΣΫτແࢹ͢Δ
IBT@NBOZͷ͕TBWF࣮ߦલʹະӬଓԽɺ ͘͠ࢠ͕ະӬଓԽͳΒ
ͷQSJNBSZ@LFZΛࢠͷGPSFJHO@LFZʹׂΓͯΔ
ࢠΛTBWF͢Δ
User#save INSERT INTO users … after_save INSERT INTO posts …
post.user_id = user.id
ؔ࿈ઌͷอଘ࿈తʹ࣮ߦ͞ΕΔ
User#save INSERT INTO users … INSERT INTO posts … Post#save
Comment#save before_save before_save INSERT INTO comments …
͜͜·Ͱͷ·ͱΊ w ؔ࿈Λఆٛ͢ΔͱɺTBWFͨ͠ͱ͖ʹؔ࿈ઌҰॹʹTBWF͢ΔͨΊͷίʔϧό οΫ͕ఆٛ͞ΕΔ w ίʔϧόοΫͷλΠϛϯάؔ࿈ʹΑͬͯҟͳΔ w ؔ࿈ઌ͕৽نϨίʔυͰ͋ΕҰॹʹอଘ͞ΕΔ ଞʹ݅͋Δ
w ؔ࿈ઌͷอଘ࿈తʹ࣮ߦ͞ΕΔ
None
IUUQTHVJEFTSVCZPOSBJMTPSHBTTPDJBUJPO@CBTJDTIUNMCJEJSFDUJPOBMBTTPDJBUJPOT
None
/
͜ͷ࣌Ͱऔಘͨ͠#PPLΦϒ δΣΫτͨͪʹରͯ͠ɺରଆͷ ؔ࿈ CPPLBVUIPS Ϩγʔό Ͱ͋ΔBVUIPSΛׂΓ͍ͯͯΔ
ͳͷͰ͜͜ͰΫΤϦ ൃߦ͞Εͳ͍
None
ઌఔͷྫͱಉ༷ʹରଆͷؔ࿈ʹର͠ ͯϨγʔόΛׂΓ͍͕ͯͨɺͲͷؔ ࿈ʹׂΓͯͨΒ͍͍͔Θ͔Βͳ͍
݁Ռͱͯ͜͜͠ͰΫΤϦ͕ൃߦ͞Εͯ / ʹͳΔ
None
໌ࣔతʹରଆͷؔ࿈໊Λࢦఆ͢Δͱ / ͕ղফ͞ΕΔ
3BJMT͔Βํؔ࿈ͷൣғ͕ ֦େͨ͠
DPO fi HBDUJWF@SFDPSEIBT@NBOZ@JOWFSTJOHUSVF ͕ͳ͍ͱ͜͜Ͱ໌ࣔతʹJOWFSTF@PGΛॻ͍ͯແޮ
͜͜·Ͱͷ·ͱΊ w ํͷؔ࿈͕ઃఆ͞Ε͍ͯΔͱ͖ʹɺ"DUJWF3FDPSEରଆͷؔ࿈ʹର ͯ͠ΦϒδΣΫτΛઃఆͯ͘͠ΕΔ w ࣗಈͰઃఆ͢Δؔ࿈Λਪଌ͢Δ͜ͱ͕Ͱ͖Δ͕ɺͰ͖ͳ͍ͱ͖ ྫ GPSFJHO@LFZΛઃఆ͍ͯ͠Δ ໌ࣔతʹJOWFSTF@PGΦϓγϣϯͰରଆͷؔ ࿈Λઃఆ͢Δඞཁ͕͋Δ
w 3BJMTҎ߱ɺIBT@NBOZͷؔ࿈ʹର͢Δํͷؔ࿈͕ޮ͘Α͏ʹͳͬͨ
None
͜͜·ͰͷલఏࣝͰ͜ͷڍಈ આ໌Ͱ͖Δ
Post#save before_save User#save INSERT INTO users … post.user_id = user.id
INSERT INTO posts … after_create Post#save before_update
Post#save before_save User#save INSERT INTO users … post.user_id = user.id
INSERT INTO posts … after_create Post#save before_update CFMPOHT@UPVTFS JOWFSTF@PGQPTUT ʹΑΓɺVTFSQPTUTʹΦϒδΣΫτׂ͕ ΓͯΒΕ͍ͯΔͷͰ͜͜Ͱؔ࿈ઌͷอଘ ͕࣮ߦ͞ΕΔ
Post#save before_save User#save INSERT INTO users … post.user_id = user.id
INSERT INTO posts … Post#save before_update after_create 1PTUͷCFGPSF@TBWFͷதͰ 1PTU͕ӬଓԽ͞ΕΔ
1PTUͷCFGPSF@TBWFͷதͰ 1PTU͕ӬଓԽ͞ΕΔ Post#save before_save User#save INSERT INTO users …
post.user_id = user.id INSERT INTO posts … Post#save before_update after_create
Post#save before_save User#save INSERT INTO users … post.user_id = user.id
INSERT INTO posts … Post#save before_update 5*14͔͜͜Β·ͨ6TFSTBWF ࣮ߦ͞Εͳ͍Α͏ʹͳ͍ͬͯ·͢ after_create
Post#save before_save User#save INSERT INTO users … post.user_id = user.id
INSERT INTO posts … Post#save before_update CFGPSF@TBWFͷޙʹ CFGPSF@ DSFBUFcVQEBUF ͕࣮ߦ͞ ΕΔ͕ɺ͜͜ͰQPTUӬଓԽ͞Εͯ ͍ΔͷͰCFGPSF@DSFBUFͰͳ͘ CFGPSF@VQEBUFʹͳΔ after_create
None
None
͏ҰͭͷʮͲ͏ͯ͜͠͏ͳͬͨʁʯ
͜ͷྫʹ͍ͭͯIUUQTCMPHXJMMOFUJO FOUSZΛಡΜͰͶ
͜͜·Ͱͷ·ͱΊ w ؔ࿈ઌΛࣗಈͰอଘ͢ΔΈͱɺJOWFSTF@PGʹΑΔํͷؔ࿈͚ʹΑ ͬͯҙਤ͍ͯ͠ͳ͍λΠϛϯάͰอଘ͕࣮ߦ͞ΕΔ͜ͱ͕͋Δ
None
3BJMT͔ΒՃ͞Εͨઃఆ
͜ͷ෦Λআͯࣗ͠ಈਪଌͯ͠ ͘ΕΔΑ͏ʹͳΔ
ࠓճͷ݅ͷΑ͏ͳ*TTVF͕ෳग़ͯ ͖ͨͷͰσϑΥϧτͰແޮʹ
͜ΕΒͷઃఆ͕ͳ͚ΕʮͲ͏ͯ͠ ͜͏ͳͬͨʁʯʹͳΓʹ͍͘
͜ΕΒͷઃఆͷϝϦοτͳΜͰ͔͢
None
QPTU͕ඥ͍͍ͮͯͳ͍ͷͰQPTUTBWF͞Εͳ͍
QPTUTBWF͞ΕΔʂ BVUPNBUJDBMMZ@JOWFSU@QMVSBM@BTTPDJBUJPOUSVF ͰΑ͍
None
VTFSCPPLTDSFBUFͩͱظ௨Γಈ͘
CPPLXSJUJOHTʹΦϒδΣΫτ͕ࣗಈ Ͱઃఆ͞Εͳ͍
໌ࣔతͳJOWFSTF@PG͘͠ BVUPNBUJDBMMZ@JOWFSU@QMVSBM@BTTPDJBUJPOUSVF Ͱظ௨Γಈ͘
͜͜·Ͱͷ·ͱΊ w ํͷؔ࿈͚ͷରΛ͛Δͱɺҙਤ͠ͳ͍ڍಈʹͭͳ͕Δ͕ٯʹ ʮ͜͏ಈ͍ͯ΄͍͠ʯͱ͍͏ڍಈ࣮ݱͰ͖Δ
ఆ2" w 2σϝϦοτ͋Δ͚ͲϝϦοτ͋Δ͜ͱ͕Θ͔ͬͨͷͰɺํͷؔ࿈ Λͬͱ༗ޮʹͯ͠Έ͍ͨͷͰ͕͢ɺ͏·͍͔͘ͳ͔ͬͨͱ͖ͷσόοά͕ ͦ͠͏Ͱ͢ w "σόοά༻ͷπʔϧΛ࡞ͬͨͷͰྑ͚Ε͍ͬͯͩ͘͞
None
None
None
ඪ४ग़ྗʹؔ࿈ͷอଘʹؔ͢Δॲཧ ͕ग़ྗ͞ΕΔ
"DUJWF3FDPSEͷϩάͱ߹Θͤͯ ͝׆༻͍ͩ͘͞
શମͷ·ͱΊ w Ͳ͏ͯ͜͠͏ͳͬͨʁͱ͍͏ྫ͔Β"DUJWF3FDPSEͷʮؔ࿈ઌΛࣗಈͰอଘͯ͘͠ΕΔػ ೳʯͱʮରଆͷؔ࿈ΛࣗಈͰઃఆͯ͘͠ΕΔػೳʯʹ͍ͭͯઆ໌͠·ͨ͠ w ͲͪΒศརͳͷͰ͕͢ɺݱঢ়Ͱཱ྆ͦ͠͏ w ͍͍ΞΠσΞ͋ͬͨΒ΄͍͠ w
ཧ۶͑͞Θ͔͍ͬͯΕͳʹ͔͋ͬͨͱ͖ͰରॲͰ͖ΔͷͰɺΈͳ͞Μ͏େৎͰ͢ Ͷ👍 w Α͘Θ͔Βͳ͍ͻͱ͓ۙ͘ͷٕज़ސ·Ͱ͝૬ஊ͍ͩ͘͞ w ͜ͷ͋ͱ4NBSU)3͞ΜͷϒʔεͰ"."ͯ͠·͢