Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Immutable ActiveRecord
Search
megane42
January 28, 2025
Programming
0
270
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
670
OSS コミットゴルフのすすめ / Let's play OSS-contribute-golf
megane42
0
98
ゆる計算理論ラジオ / P vs NP for beginner
megane42
1
240
How to Make "DJ giftee"
megane42
1
940
Rails 6 Upgrade "Practical" Guide
megane42
6
1.4k
updated_at に依存したら大変なことになった / Don't depend on updated_at
megane42
0
600
本当は怖い Rails の `build_xxx` / The Hard Facts of `build_xxx` of Rails
megane42
0
260
Other Decks in Programming
See All in Programming
AWS CDKの推しポイントN選
akihisaikeda
1
240
ZOZOにおけるAI活用の現在 ~モバイルアプリ開発でのAI活用状況と事例~
zozotech
PRO
8
5.4k
複数人でのCLI/Infrastructure as Codeの暮らしを良くする
shmokmt
5
2.2k
ハイパーメディア駆動アプリケーションとIslandアーキテクチャ: htmxによるWebアプリケーション開発と動的UIの局所的適用
nowaki28
0
380
俺流レスポンシブコーディング 2025
tak_dcxi
14
8.4k
無秩序からの脱却 / Emergence from chaos
nrslib
2
13k
Navigation 3: 적응형 UI를 위한 앱 탐색
fornewid
1
210
FluorTracer / RayTracingCamp11
kugimasa
0
220
テストやOSS開発に役立つSetup PHP Action
matsuo_atsushi
0
150
モデル駆動設計をやってみようワークショップ開催報告(Modeling Forum2025) / model driven design workshop report
haru860
0
250
STYLE
koic
0
140
Socio-Technical Evolution: Growing an Architecture and Its Organization for Fast Flow
cer
PRO
0
310
Featured
See All Featured
Git: the NoSQL Database
bkeepers
PRO
432
66k
[SF Ruby Conf 2025] Rails X
palkan
0
480
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.5k
Java REST API Framework Comparison - PWX 2021
mraible
34
9k
Documentation Writing (for coders)
carmenintech
76
5.2k
Product Roadmaps are Hard
iamctodd
PRO
55
12k
Building an army of robots
kneath
306
46k
Learning to Love Humans: Emotional Interface Design
aarron
274
41k
Designing for Performance
lara
610
69k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
1
92
Bash Introduction
62gerente
615
210k
Balancing Empowerment & Direction
lara
5
790
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 の実装方法が露出しすぎという話もある