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
1.1k
どうしてこうなった?から理解する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
Sidekiq vs Solid Queue
willnet
13
9.6k
Exceptional Rails
willnet
6
6.9k
Breaking the Flaky Test Cycle
willnet
2
2k
mrskで広がるインフラの選択肢
willnet
1
1k
アプリケーションを長期にわたって無理なく運用するためのたったひとつの方法
willnet
2
2.1k
HotwireからDHHが考えるこれからのRailsとJSの付き合い方を知る
willnet
14
13k
Rails6.1で新しく入る機能について
willnet
12
15k
Concerns about Concerns
willnet
11
34k
Clean Test Code Revised
willnet
34
18k
Other Decks in Programming
See All in Programming
As an Engineers, let's build the CRM system via LINE Official Account 2.0
clonn
1
670
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
tomoki10
0
190
Zoneless Testing
rainerhahnekamp
0
120
Monixと常駐プログラムの勘どころ / Scalaわいわい勉強会 #4
stoneream
0
270
fs2-io を試してたらバグを見つけて直した話
chencmd
0
220
プロダクトの品質に コミットする / Commit to Product Quality
pekepek
2
770
talk-with-local-llm-with-web-streams-api
kbaba1001
0
170
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1k
menu基盤チームによるGoogle Cloudの活用事例~Application Integration, Cloud Tasks編~
yoshifumi_ishikura
0
110
Discord Bot with AI -for English learners-
xin9le
1
120
これが俺の”自分戦略” プロセスを楽しんでいこう! - Developers CAREER Boost 2024
niftycorp
PRO
0
190
103 Early Hints
sugi_0000
1
220
Featured
See All Featured
Producing Creativity
orderedlist
PRO
341
39k
Scaling GitHub
holman
458
140k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Music & Morning Musume
bryan
46
6.2k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Code Reviewing Like a Champion
maltzj
520
39k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
Facilitating Awesome Meetings
lara
50
6.1k
Documentation Writing (for coders)
carmenintech
66
4.5k
Making the Leap to Tech Lead
cromwellryan
133
9k
Why Our Code Smells
bkeepers
PRO
335
57k
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͞ΜͷϒʔεͰ"."ͯ͠·͢