Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Immutable ActiveRecord
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
megane42
January 28, 2025
Programming
380
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Immutable ActiveRecord
megane42
January 28, 2025
More Decks by megane42
See All by megane42
Rails deprecation warning に立ち向かう技術 / v.s. rails deprecation warnings
megane42
0
860
OSS コミットゴルフのすすめ / Let's play OSS-contribute-golf
megane42
0
150
ゆる計算理論ラジオ / P vs NP for beginner
megane42
1
300
How to Make "DJ giftee"
megane42
1
1k
Rails 6 Upgrade "Practical" Guide
megane42
6
1.4k
updated_at に依存したら大変なことになった / Don't depend on updated_at
megane42
0
650
本当は怖い Rails の `build_xxx` / The Hard Facts of `build_xxx` of Rails
megane42
0
310
Other Decks in Programming
See All in Programming
はじめてのPLATEAU
nokonoko1203
1
350
SFCで実現する病院・医療システムの動的組み立て式UI
koukimiura
1
170
Web Framework Benchmark Battle 2026, Any%
a_matsuda
3
3.1k
Optunaを作ったときにやりたかったこと、その後 (2026/10/16, Optuna Meetup #3)
iwiwi
4
1.4k
クラウドゲーミングのパフォーマンス最適化に挑む / Cloud Gaming Performance Tuning
vivion
0
320
Seven New Principles of Software Engineering
yegor256
0
440
Spec-driven Development with AI
simas
PRO
0
2.2k
クラウドエージェントは、 何がそんなに便利なのか / what makes cloud agents so useful
rkaga
5
730
プロジェクトコードをキレイキレイしよう!暗黙的な参照を読み解く静的解析
ostk0069
0
200
私なりのハーネスエンジニアリング ~独自の言語処理系の開発を通して~
seosoft
0
180
Modernizing iOS Apps
seyfoyun
0
150
Inertia Rails: The One-Person Framework with React & Friends
skryukov
0
380
Featured
See All Featured
Tell your own story through comics
letsgokoyo
1
1.1k
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
2
1.2k
Thoughts on Productivity
jonyablonski
76
5.4k
Chasing Engaging Ingredients in Design
codingconduct
0
390
How to build a perfect <img>
jonoalderson
1
6.2k
Git: the NoSQL Database
bkeepers
PRO
433
67k
Ethics towards AI in product and experience design
skipperchong
2
460
Writing Fast Ruby
sferik
630
63k
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
23k
Evolving SEO for Evolving Search Engines
ryanjones
0
350
What does AI have to do with Human Rights?
axbom
PRO
1
2.5k
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 の実装方法が露出しすぎという話もある