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
Immutable ActiveRecord
Search
megane42
January 28, 2025
Programming
0
160
Immutable ActiveRecord
megane42
January 28, 2025
Tweet
Share
More Decks by megane42
See All by megane42
Rails deprecation warning に立ち向かう技術 / v.s. rails deprecation warnings
megane42
0
570
OSS コミットゴルフのすすめ / Let's play OSS-contribute-golf
megane42
0
84
ゆる計算理論ラジオ / P vs NP for beginner
megane42
1
200
How to Make "DJ giftee"
megane42
1
870
Rails 6 Upgrade "Practical" Guide
megane42
6
1.3k
updated_at に依存したら大変なことになった / Don't depend on updated_at
megane42
0
530
本当は怖い Rails の `build_xxx` / The Hard Facts of `build_xxx` of Rails
megane42
0
170
Other Decks in Programming
See All in Programming
Cursor/Devin全社導入の理想と現実
saitoryc
28
21k
20250426 GDGoC 合同新歓 - GDGoC のススメ
getty708
0
100
プロダクト横断分析に役立つ、事前集計しないサマリーテーブル設計
hanon52_
3
530
The Missing Link in Angular’s Signal Story: Resource API and httpResource
manfredsteyer
PRO
0
140
一緒に働きたくなるプログラマの思想 #QiitaConference
mu_zaru
78
20k
Dissecting and Reconstructing Ruby Syntactic Structures
ydah
3
1.9k
2ヶ月で生産性2倍、お買い物アプリ「カウシェ」4チーム同時改善の取り組み
ike002jp
1
110
SwiftDataのカスタムデータストアを試してみた
1mash0
0
140
[NG India] Event-Based State Management with NgRx SignalStore
markostanimirovic
1
190
KawaiiLT 登壇資料 キャリアとモチベーション
hiiragi
0
160
RubyKaigi Dev Meeting 2025
tenderlove
1
1.2k
generative-ai-use-cases(GenU)の推しポイント ~2025年4月版~
hideg
1
370
Featured
See All Featured
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2k
The Straight Up "How To Draw Better" Workshop
denniskardys
233
140k
It's Worth the Effort
3n
184
28k
Music & Morning Musume
bryan
47
6.5k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.7k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
Fontdeck: Realign not Redesign
paulrobertlloyd
84
5.5k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
45
7.2k
Raft: Consensus for Rubyists
vanstee
137
6.9k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
357
30k
The Power of CSS Pseudo Elements
geoffreycrofte
75
5.8k
Transcript
[翻訳] Immutable ActiveRecord 2025/01/28 Hikaru Kazama @ Gotanda.rb#61
自己紹介 • 名前:Hikaru Kazama (@megane42) • 職場:株式会社ギフティ • 趣味:かっこいいワンタイムパスワード集め
参考記事 • Nathan Kallman 氏のブログをほぼそのまま引用しただけ • https://www.kallmanation.com/immutable-activerecord
背景 • PointCard という ActiveRecord クラスに状態を持たせたい • 有効 • 無効
• 凍結
背景 • PointCard に status カラムを作るんじゃなくて、 ステータス変更イベントレコードを積み上げて、現在ステータ スはそこから「導出」するようにしたい!
こんな感じ • PointCardStatusChanging • id • point_card_id • status •
changed_at
課題 • PointCardStatusChanging が mutable である
解法: ActiveRecord::Core#readonly? • ActiveRecord::Core に実装されていて、 すべての AR オブジェクトに対して実行できる • #readonly?
はデフォルトで false を返す • #readonly! を呼び出すと、以後 true を返すようになる
解法: ActiveRecord::Core#readonly? • ActiveRecord は #create, #update, #destroy のたびに #readonly?
を実行しており、戻り値が true だったら ActiveRecord::ReadOnlyRecord エラーを起こす
ということは • Immutable にしたクラスにこんなメソッドを定義(オーバー ライド)してやれば、新規作成以外の変更ができなくなる!
注意点 • update_columns みたいな「AR コールバックが実行されない メソッド」を実行したときは無力
まとめ • AR にはオブジェクトを read only にする機構が備わっている • #new_record? とのコンボでいい感じに
immutable にできる
おまけ: 別の悩み • 状態遷移ルールを無視してイベントレコードを作れてしまう • 例えば、一度「凍結」したら元には戻れないとする 有効 無効 凍結
おまけ: 別の悩み
おまけ: 別の悩み • 実際は PointCard#activate! とかを実装するんだろうけど、 ガン無視して PointCardStatusChanging.create されること を誰も止められない
おまけ: 別の悩み • PointCardStatusChanging のバリデーションとして状態遷移 を実装することもできるけど、それはそれで大変 • status の実装方法が露出しすぎという話もある