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.3k
どうしてこうなった?から理解する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
14
10k
Exceptional Rails
willnet
6
7k
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
為你自己學 Python
eddie
0
520
Package Traits
ikesyo
1
210
Fibonacci Function Gallery - Part 2
philipschwarz
PRO
0
210
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
390
Androidアプリのモジュール分割における:x:commonを考える
okuzawats
1
280
functionalなアプローチで動的要素を排除する
ryopeko
1
200
混沌とした例外処理とエラー監視に秩序をもたらす
morihirok
13
2.2k
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
300
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
1.4k
Alba: Why, How and What's So Interesting
okuramasafumi
0
210
watsonx.ai Dojo #6 継続的なAIアプリ開発と展開
oniak3ibm
PRO
0
170
DevinとCursorから学ぶAIエージェントメモリーの設計とMoatの考え方
itarutomy
0
140
Featured
See All Featured
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
3
180
Product Roadmaps are Hard
iamctodd
PRO
50
11k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
The Cost Of JavaScript in 2023
addyosmani
46
7.2k
GraphQLの誤解/rethinking-graphql
sonatard
68
10k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.4k
Reflections from 52 weeks, 52 projects
jeffersonlam
348
20k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.3k
Building Adaptive Systems
keathley
38
2.4k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.2k
Building Flexible Design Systems
yeseniaperezcruz
328
38k
Designing Experiences People Love
moore
139
23k
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͞ΜͷϒʔεͰ"."ͯ͠·͢