Upgrade to Pro — share decks privately, control downloads, hide ads and more …

どうしてこうなった?から理解するActive Recordの関連の裏側

どうしてこうなった?から理解するActive Recordの関連の裏側

大阪Ruby会議04 https://rubykansai.github.io/osaka04/ での発表内容です。

Shinichi Maeshima

August 24, 2024
Tweet

More Decks by Shinichi Maeshima

Other Decks in Programming

Transcript

  1. Active Record is like a magic w ΈΜͳେ޷͖"DUJWF3FDPSE w ن໿ʹΑΓɺ·ΔͰຐ๏ͷΑ͏ʹಈ͘

    w ͜Μͳײ͡Ͱಈ͔͘ͳʁͱࢥͬͨΒ͍͍ͩͨͦͷΑ͏ʹಈ͘ w ͕ɺۃكʹͦ͏Ͱ͸ͳ͍͜ͱ΋͋Δ
  2. User#save INSERT INTO users … INSERT INTO posts … Post#save

    Comment#save before_save before_save INSERT INTO comments …
  3. / 

  4. Post#save before_save User#save INSERT INTO users … post.user_id = user.id

    INSERT INTO posts … after_create Post#save before_update
  5. 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ʹΦϒδΣΫτׂ͕ Γ౰ͯΒΕ͍ͯΔͷͰ͜͜Ͱؔ࿈ઌͷอଘ ͕࣮ߦ͞ΕΔ
  6. 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͕ӬଓԽ͞ΕΔ 
  7. 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
  8. 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
  9. 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
  10. શମͷ·ͱΊ w Ͳ͏ͯ͜͠͏ͳͬͨʁͱ͍͏ྫ͔Β"DUJWF3FDPSEͷʮؔ࿈ઌΛࣗಈͰอଘͯ͘͠ΕΔػ ೳʯͱʮ൓ରଆͷؔ࿈ΛࣗಈͰઃఆͯ͘͠ΕΔػೳʯʹ͍ͭͯઆ໌͠·ͨ͠ w ͲͪΒ΋ศརͳͷͰ͕͢ɺݱঢ়Ͱ͸ཱ྆͸೉ͦ͠͏ w ͍͍ΞΠσΞ͋ͬͨΒ΄͍͠  w

    ཧ۶͑͞Θ͔͍ͬͯΕ͹ͳʹ͔͋ͬͨͱ͖Ͱ΋ରॲͰ͖ΔͷͰɺΈͳ͞Μ͸΋͏େৎ෉Ͱ͢ Ͷ👍 w Α͘Θ͔Βͳ͍ͻͱ͸͓ۙ͘ͷٕज़ސ໰·Ͱ͝૬ஊ͍ͩ͘͞ w ͜ͷ͋ͱ4NBSU)3͞ΜͷϒʔεͰ"."ͯ͠·͢