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
560
どうしてこうなった?から理解する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.6k
Breaking the Flaky Test Cycle
willnet
2
1.9k
mrskで広がるインフラの選択肢
willnet
1
950
アプリケーションを長期にわたって無理なく運用するためのたったひとつの方法
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
Kotlin 2.0が与えるAndroid開発の進化
masayukisuda
1
260
Lessons by WebAssembly app in production on CDN Edge Computing Service
tetsuharuohzeki
0
150
What we keep in mind when migrating from Serverless Framework to AWS CDK and AWS SAM
kasacchiful
1
140
rbs-inlineを導入してYARDからRBSに移行する
euglena1215
1
250
The Sequel to a Dream of Ruby Parser's Grammar
ydah
1
210
dRuby 入門者によるあなたの身近にあるdRuby 入門
makicamel
4
350
Hono・Prisma・AWSでGeoなAPI開発
nokonoko1203
5
660
Rustではじめる負荷試験
skanehira
5
1.2k
[DroidKaigi 2024] Android ViewからJetpack Composeへ 〜Jetpack Compose移行のすゝめ〜 / From Android View to Jetpack Compose: A Guide to Migration
syarihu
1
240
令和トラベルにおけるLLM活用事例:社内ツール開発から得た学びと実践
ippo012
0
120
What is Parser
yui_knk
9
4k
エンジニア1年目で複雑なコードの改善に取り組んだ話
mtnmr
3
1.8k
Featured
See All Featured
Thoughts on Productivity
jonyablonski
66
4.2k
The Brand Is Dead. Long Live the Brand.
mthomps
53
37k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
157
15k
Building Adaptive Systems
keathley
36
2.1k
How to Think Like a Performance Engineer
csswizardry
16
940
Art, The Web, and Tiny UX
lynnandtonic
294
20k
5 minutes of I Can Smell Your CMS
philhawksworth
201
19k
Optimising Largest Contentful Paint
csswizardry
29
2.8k
Imperfection Machines: The Place of Print at Facebook
scottboms
263
13k
The Invisible Side of Design
smashingmag
295
50k
The Pragmatic Product Professional
lauravandoore
31
6.2k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
36
1.9k
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͞ΜͷϒʔεͰ"."ͯ͠·͢