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
330
0
Share
Immutable ActiveRecord
megane42
January 28, 2025
More Decks by megane42
See All by megane42
Rails deprecation warning に立ち向かう技術 / v.s. rails deprecation warnings
megane42
0
780
OSS コミットゴルフのすすめ / Let's play OSS-contribute-golf
megane42
0
120
ゆる計算理論ラジオ / P vs NP for beginner
megane42
1
270
How to Make "DJ giftee"
megane42
1
980
Rails 6 Upgrade "Practical" Guide
megane42
6
1.4k
updated_at に依存したら大変なことになった / Don't depend on updated_at
megane42
0
630
本当は怖い Rails の `build_xxx` / The Hard Facts of `build_xxx` of Rails
megane42
0
280
Other Decks in Programming
See All in Programming
Agent Skills を社内で育てる仕組み作り
jackchuka
1
1.8k
【26新卒研修資料】TDD実装演習
dip_tech
PRO
0
180
Explore CoroutineScope
tomoeng11
0
180
ついに来た!本格的なマルチクラウド時代の Google Cloud
maroon1st
0
400
when storing skills in S3 file
watany
3
1.5k
20年以上続くプロダクトでも使い続けられる静的解析ツールを求めて
matsuo_atsushi
0
140
mruby on C#: From VM Implementation to Game Scripting (RubyKaigi 2026)
hadashia
2
1.7k
Building on Bluesky's AT Protocol with Ruby
mackuba
0
110
Agentic UI in the Frontend: Architectures with Open Standards @JAX 2026 in Mainz
manfredsteyer
PRO
0
110
「OSSがあるなら自作するな」は AI時代も正しいか ── Build vs Adopt の新しい判断基準
kumorn5s
7
2.5k
ハーネスエンジニアリングにどう向き合うか 〜ルールファイルを超えて開発プロセスを設計する〜 / How to approach harness engineering
rkaga
28
19k
決定論 vs 確率論:Gemini 3 FlashとTF-IDFを組み合わせた「法規判定エンジン」の構築
shukob
0
160
Featured
See All Featured
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
55k
Typedesign – Prime Four
hannesfritz
42
3k
RailsConf 2023
tenderlove
30
1.4k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
440
It's Worth the Effort
3n
188
29k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.1k
Mind Mapping
helmedeiros
PRO
1
190
Fireside Chat
paigeccino
42
3.9k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
110
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
300
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.8k
Ruling the World: When Life Gets Gamed
codingconduct
0
220
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 の実装方法が露出しすぎという話もある