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
940
どうしてこうなった?から理解する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
8.8k
Exceptional Rails
willnet
6
6.9k
Breaking the Flaky Test Cycle
willnet
2
2k
mrskで広がるインフラの選択肢
willnet
1
980
アプリケーションを長期にわたって無理なく運用するためのたったひとつの方法
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
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.6k
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
170
みんなでプロポーザルを書いてみた
yuriko1211
0
260
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.2k
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
920
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.4k
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
930
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
CSC509 Lecture 12
javiergs
PRO
0
160
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
470
Featured
See All Featured
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Embracing the Ebb and Flow
colly
84
4.5k
Typedesign – Prime Four
hannesfritz
40
2.4k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
Designing Experiences People Love
moore
138
23k
Done Done
chrislema
181
16k
How to Ace a Technical Interview
jacobian
276
23k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
A Modern Web Designer's Workflow
chriscoyier
693
190k
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͞ΜͷϒʔεͰ"."ͯ͠·͢