Slide 1

Slide 1 text

Osaka RubyKaigi 04 @willnet Shinichi Maeshima Ͳ͏ͯ͜͠͏ͳͬͨʁ ͔Βཧղ͢Δ Active Recordͷཪଆ

Slide 2

Slide 2 text

Shinichi Maeshima    @netwillnet @willnet https://blog.willnet.in Willnet Inc.

Slide 3

Slide 3 text

ੲ೥͚ͩେࡕʹॅΜͰ͍·ͨ͠

Slide 4

Slide 4 text

HJO[BSC΍ͬͯ·͢

Slide 5

Slide 5 text

ٕज़ސ໰ۀΛ͍ͯ͠·͢

Slide 6

Slide 6 text

͓࢓ࣄ4/4తͳ΋ͷΛ։ൃ ͍ͯ͠·͢

Slide 7

Slide 7 text

8F`SFOPUIJSJOH

Slide 8

Slide 8 text

ސ໰ઌʹ͍ͭͯ͸؇ื

Slide 9

Slide 9 text

େࡕ3VCZձٞʹ ొஃ͢Δͷ͸ճ໨

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

աڈ࡞ͷࢿྉ͸ 4QFBLFS%FDL΍ϒϩάͰ ֬ೝͰ͖·͢

Slide 13

Slide 13 text

͔͜͜Βຊ୊

Slide 14

Slide 14 text

Osaka RubyKaigi 04 @willnet Ͳ͏ͯ͜͠͏ͳͬͨʁ ͔Βཧղ͢Δ Active Recordͷཪଆ

Slide 15

Slide 15 text

Osaka RubyKaigi 04 @willnet Ͳ͏ͯ͜͠͏ͳͬͨʁ ͔Βཧղ͢Δ Active Recordͷཪଆ

Slide 16

Slide 16 text

Active Record is like a magic w ΈΜͳେ޷͖"DUJWF3FDPSE w ن໿ʹΑΓɺ·ΔͰຐ๏ͷΑ͏ʹಈ͘ w ͜Μͳײ͡Ͱಈ͔͘ͳʁͱࢥͬͨΒ͍͍ͩͨͦͷΑ͏ʹಈ͘ w ͕ɺۃكʹͦ͏Ͱ͸ͳ͍͜ͱ΋͋Δ

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

ؔ࿈Λఆٛ͢Δͱಈతʹϝιου ͕ੜ੒͞ΕΔ

Slide 22

Slide 22 text

ίʔϧόοΫ΋ੜ੒͞Ε͍ͯΔ

Slide 23

Slide 23 text

ʮϞσϧอଘ࣌ʹؔ࿈ઌ΋อଘ͢Δʯ Λ͢ΔίʔϧόοΫ

Slide 24

Slide 24 text

͜ΕΛ࣮ߦͨ͠ͱ͖ͷڍಈΛઆ໌͠·͢

Slide 25

Slide 25 text

BVUPTBWF@BTTPDJBUFE@SFDPSET @GPS@VTFSͷ࣮ࡍͷॲཧ

Slide 26

Slide 26 text

͍Ζ͍Ζলུͨ͠ίʔυ

Slide 27

Slide 27 text

ओʹBVUPTBWFUSVF DPNQPTJUF@QSJNBSZ@LFZTͳͲΛলུ͍ͯ͠·͢

Slide 28

Slide 28 text

ؔ࿈ͷϝλ৘ใ

Slide 29

Slide 29 text

CFMPOHT@UPVTFSͳͷͰVTFS͕ฦͬͯ͘Δ lVTFS@JEz͕ฦͬͯ͘Δ

Slide 30

Slide 30 text

ϞσϧΦϒδΣΫτຖʹੜ੒͞ΕΔؔ࿈ΦϒδΣΫτɻ 1PTUVTFSͳͲͷؔ࿈ϝιου͸ શ෦͜ͷΦϒδΣΫτܦ༝Ͱ࣮ߦ͞ΕΔɻ ؔ࿈ઌͷΦϒδΣΫτ΋࣋ͭɻ

Slide 31

Slide 31 text

ؔ࿈ઌͷΦϒδΣΫτ͕ඥ͍ͮͯͳ͔ͬͨΒ ͳʹ΋͠ͳ͍

Slide 32

Slide 32 text

ؔ࿈ઌͷΦϒδΣΫτ͕ ӬଓԽ͞Ε͍ͯͳ͔ͬͨΒTBWF

Slide 33

Slide 33 text

ؔ࿈ઌͷQSJNBSZ@LFZΛ ࣗ਎ͷ֎෦Ωʔͱͯ͠ઃఆ͢Δ

Slide 34

Slide 34 text

ίʔϧόοΫ࣮ߦޙʹΫΤϦ͕ൃߦ ͞ΕΔ Post#save before_save User#save INSERT INTO users … post.user_id = user.id INSERT INTO posts …

Slide 35

Slide 35 text

IBT@NBOZ΋େ·͔ͳ࢓૊Έ͸ ಉ͡

Slide 36

Slide 36 text

λΠϛϯά͸ҧ͏

Slide 37

Slide 37 text

5*14ίʔϧόοΫͷൃՐॱ൪͸ BGUFS@ DSFBUFcVQEBUF BGUFS@TBWFͷॱ

Slide 38

Slide 38 text

͜ΕΛ࣮ߦͨ͠ͱ͖ͷڍಈΛઆ໌͠·͢

Slide 39

Slide 39 text

BVUPTBWF@BTTPDJBUFE@SFDPSET @GPS@QPTUTͷ࣮ࡍͷॲཧ

Slide 40

Slide 40 text

͍Ζ͍Ζলུͨ͠ίʔυ

Slide 41

Slide 41 text

ϩʔυࡁΈͷؔ࿈ઌͷ͏ͪɺ৚݅ʹϚον͢Δ΋ͷΛऔಘ͢Δ

Slide 42

Slide 42 text

࡟আࡁΈͷΦϒδΣΫτ͸ແࢹ͢Δ

Slide 43

Slide 43 text

IBT@NBOZͷ਌͕TBWF࣮ߦલʹະӬଓԽɺ ΋͘͠͸ࢠ͕ະӬଓԽͳΒ

Slide 44

Slide 44 text

਌ͷQSJNBSZ@LFZΛࢠͷGPSFJHO@LFZʹׂΓ౰ͯΔ

Slide 45

Slide 45 text

ࢠΛTBWF͢Δ

Slide 46

Slide 46 text

User#save INSERT INTO users … after_save INSERT INTO posts … post.user_id = user.id

Slide 47

Slide 47 text

ؔ࿈ઌͷอଘ͸࿈࠯తʹ࣮ߦ͞ΕΔ

Slide 48

Slide 48 text

User#save INSERT INTO users … INSERT INTO posts … Post#save Comment#save before_save before_save INSERT INTO comments …

Slide 49

Slide 49 text

͜͜·Ͱͷ·ͱΊ w ؔ࿈Λఆٛ͢ΔͱɺTBWFͨ͠ͱ͖ʹؔ࿈ઌ΋ҰॹʹTBWF͢ΔͨΊͷίʔϧό οΫ͕ఆٛ͞ΕΔ w ίʔϧόοΫͷλΠϛϯά͸ؔ࿈ʹΑͬͯҟͳΔ w ؔ࿈ઌ͕৽نϨίʔυͰ͋Ε͹Ұॹʹอଘ͞ΕΔ ஫ଞʹ΋৚݅͸͋Δ w ؔ࿈ઌͷอଘ͸࿈࠯తʹ࣮ߦ͞ΕΔ

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

IUUQTHVJEFTSVCZPOSBJMTPSHBTTPDJBUJPO@CBTJDTIUNMCJEJSFDUJPOBMBTTPDJBUJPOT

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

/

Slide 54

Slide 54 text

͜ͷ࣌఺Ͱऔಘͨ͠#PPLΦϒ δΣΫτͨͪʹରͯ͠ɺ൓ରଆͷ ؔ࿈ CPPLBVUIPS ΁Ϩγʔό Ͱ͋ΔBVUIPSΛׂΓ౰͍ͯͯΔ

Slide 55

Slide 55 text

ͳͷͰ͜͜Ͱ͸ΫΤϦ͸ ൃߦ͞Εͳ͍

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

ઌఔͷྫͱಉ༷ʹ൓ରଆͷؔ࿈ʹର͠ ͯϨγʔόΛׂΓ౰͍͕ͯͨɺͲͷؔ ࿈ʹׂΓ౰ͯͨΒ͍͍͔Θ͔Βͳ͍

Slide 58

Slide 58 text

݁Ռͱͯ͜͜͠ͰΫΤϦ͕ൃߦ͞Εͯ /ʹͳΔ

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

໌ࣔతʹ൓ରଆͷؔ࿈໊Λࢦఆ͢Δͱ /͕ղফ͞ΕΔ

Slide 61

Slide 61 text

3BJMT͔Β૒ํ޲ؔ࿈ͷൣғ͕ ֦େͨ͠

Slide 62

Slide 62 text

DPO fi HBDUJWF@SFDPSEIBT@NBOZ@JOWFSTJOHUSVF ͕ͳ͍ͱ͜͜Ͱ໌ࣔతʹJOWFSTF@PGΛॻ͍ͯ΋ແޮ

Slide 63

Slide 63 text

͜͜·Ͱͷ·ͱΊ w ૒ํ޲ͷؔ࿈͕ઃఆ͞Ε͍ͯΔͱ͖ʹɺ"DUJWF3FDPSE͸൓ରଆͷؔ࿈ʹର ͯ͠ΦϒδΣΫτΛઃఆͯ͘͠ΕΔ w ࣗಈͰઃఆ͢Δؔ࿈Λਪଌ͢Δ͜ͱ͕Ͱ͖Δ͕ɺͰ͖ͳ͍ͱ͖ ྫ GPSFJHO@LFZΛઃఆ͍ͯ͠Δ ͸໌ࣔతʹJOWFSTF@PGΦϓγϣϯͰ൓ରଆͷؔ ࿈Λઃఆ͢Δඞཁ͕͋Δ w 3BJMTҎ߱͸ɺIBT@NBOZͷؔ࿈ʹର͢Δ૒ํ޲ͷؔ࿈͕ޮ͘Α͏ʹͳͬͨ

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

͜͜·Ͱͷલఏ஌ࣝͰ͜ͷڍಈ͸ આ໌Ͱ͖Δ

Slide 66

Slide 66 text

Post#save before_save User#save INSERT INTO users … post.user_id = user.id INSERT INTO posts … after_create Post#save before_update

Slide 67

Slide 67 text

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ʹΦϒδΣΫτׂ͕ Γ౰ͯΒΕ͍ͯΔͷͰ͜͜Ͱؔ࿈ઌͷอଘ ͕࣮ߦ͞ΕΔ

Slide 68

Slide 68 text

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͕ӬଓԽ͞ΕΔ

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

΋͏ҰͭͷʮͲ͏ͯ͜͠͏ͳͬͨʁʯ

Slide 75

Slide 75 text

͜ͷྫʹ͍ͭͯ͸IUUQTCMPHXJMMOFUJO FOUSZΛಡΜͰͶ

Slide 76

Slide 76 text

͜͜·Ͱͷ·ͱΊ w ؔ࿈ઌΛࣗಈͰอଘ͢Δ࢓૊ΈͱɺJOWFSTF@PGʹΑΔ૒ํ޲ͷؔ࿈෇͚ʹΑ ͬͯҙਤ͍ͯ͠ͳ͍λΠϛϯάͰอଘ͕࣮ߦ͞ΕΔ͜ͱ͕͋Δ

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

3BJMT͔Β௥Ճ͞Εͨઃఆ

Slide 79

Slide 79 text

͜ͷ෦෼Λ࡟আͯ͠΋ࣗಈਪଌͯ͠ ͘ΕΔΑ͏ʹͳΔ

Slide 80

Slide 80 text

ࠓճͷ݅ͷΑ͏ͳ*TTVF͕ෳ਺ग़ͯ ͖ͨͷͰσϑΥϧτͰ͸ແޮʹ

Slide 81

Slide 81 text

͜ΕΒͷઃఆ͕ͳ͚Ε͹ʮͲ͏ͯ͠ ͜͏ͳͬͨʁʯʹ͸ͳΓʹ͍͘

Slide 82

Slide 82 text

͜ΕΒͷઃఆͷϝϦοτͳΜͰ͔͢

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

QPTU͕ඥ͍͍ͮͯͳ͍ͷͰQPTU͸TBWF͞Εͳ͍

Slide 85

Slide 85 text

QPTU΋TBWF͞ΕΔʂ BVUPNBUJDBMMZ@JOWFSU@QMVSBM@BTTPDJBUJPOUSVF Ͱ΋Α͍

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

VTFSCPPLTDSFBUFͩͱظ଴௨Γಈ͘

Slide 88

Slide 88 text

CPPLXSJUJOHTʹΦϒδΣΫτ͕ࣗಈ Ͱઃఆ͞Εͳ͍

Slide 89

Slide 89 text

໌ࣔతͳJOWFSTF@PG΋͘͠͸ BVUPNBUJDBMMZ@JOWFSU@QMVSBM@BTTPDJBUJPOUSVF Ͱظ଴௨Γಈ͘

Slide 90

Slide 90 text

͜͜·Ͱͷ·ͱΊ w ૒ํ޲ͷؔ࿈෇͚ͷର৅Λ޿͛Δͱɺҙਤ͠ͳ͍ڍಈʹ΋ͭͳ͕Δ͕ٯʹ ʮ͜͏ಈ͍ͯ΄͍͠ʯͱ͍͏ڍಈ΋࣮ݱͰ͖Δ

Slide 91

Slide 91 text

૝ఆ2" w 2σϝϦοτ΋͋Δ͚ͲϝϦοτ΋͋Δ͜ͱ͕Θ͔ͬͨͷͰɺ૒ํ޲ͷؔ࿈ Λ΋ͬͱ༗ޮʹͯ͠Έ͍ͨͷͰ͕͢ɺ͏·͍͔͘ͳ͔ͬͨͱ͖ͷσόοά͕೉ ͦ͠͏Ͱ͢ w "σόοά༻ͷπʔϧΛ࡞ͬͨͷͰྑ͚Ε͹࢖͍ͬͯͩ͘͞

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

ඪ४ग़ྗʹؔ࿈ͷอଘʹؔ͢Δॲཧ ͕ग़ྗ͞ΕΔ

Slide 96

Slide 96 text

"DUJWF3FDPSEͷϩάͱ߹Θͤͯ ͝׆༻͍ͩ͘͞

Slide 97

Slide 97 text

શମͷ·ͱΊ w Ͳ͏ͯ͜͠͏ͳͬͨʁͱ͍͏ྫ͔Β"DUJWF3FDPSEͷʮؔ࿈ઌΛࣗಈͰอଘͯ͘͠ΕΔػ ೳʯͱʮ൓ରଆͷؔ࿈ΛࣗಈͰઃఆͯ͘͠ΕΔػೳʯʹ͍ͭͯઆ໌͠·ͨ͠ w ͲͪΒ΋ศརͳͷͰ͕͢ɺݱঢ়Ͱ͸ཱ྆͸೉ͦ͠͏ w ͍͍ΞΠσΞ͋ͬͨΒ΄͍͠ w ཧ۶͑͞Θ͔͍ͬͯΕ͹ͳʹ͔͋ͬͨͱ͖Ͱ΋ରॲͰ͖ΔͷͰɺΈͳ͞Μ͸΋͏େৎ෉Ͱ͢ Ͷ👍 w Α͘Θ͔Βͳ͍ͻͱ͸͓ۙ͘ͷٕज़ސ໰·Ͱ͝૬ஊ͍ͩ͘͞ w ͜ͷ͋ͱ4NBSU)3͞ΜͷϒʔεͰ"."ͯ͠·͢