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
Rails Context Validation
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
kbaba1001
December 08, 2014
Technology
820
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Rails Context Validation
Validation Night (
http://connpass.com/event/9982/
)
kbaba1001
December 08, 2014
More Decks by kbaba1001
See All by kbaba1001
How to build a video conferencing system that no one has ever told you about
kbaba1001
0
84
Build React system with ClojureScript (Squint)
kbaba1001
0
210
talk-with-local-llm-with-web-streams-api
kbaba1001
0
570
Lume: Static Site Generator
kbaba1001
0
750
React_2023
kbaba1001
0
220
Word Penne
kbaba1001
0
260
I live by using a minor language
kbaba1001
1
230
fast optical line
kbaba1001
0
430
ArtPosePro and Procreate
kbaba1001
1
270
Other Decks in Technology
See All in Technology
Rust で関数型ドメインモデリング
takonda
0
170
アウトプット初心者、半年間のふりかえり
4o1sobblue
0
460
Let's Change Our World—Again
snoozer05
PRO
2
200
React改造してみた
tsukina_7mochi
0
140
Local CI × GitHub Actions
gogutan
3
240
SAENM ~Jupyterで残すPoCの試行錯誤、未来の自分と仲間を助ける記録!~
terapyon
0
170
フロントエンドカンファレンス関西2026 - 速すぎるUIに、待ち時間を
teamlab
PRO
0
220
LLM-as-a-Judgeは本当に信頼できるのか――評価器を評価する話
taka_aki
2
950
LLM Content Moderationを支える評価・改善ループの実践
suikabar
1
250
Rustuna: Rustによる高速なOptuna実装
pfn
PRO
0
210
Post AI時代のリファクタリング実践記 / Refactoring in the post AI era
kaminashi
1
310
AI導入は済んだ。次は統制を作り替える番だ_AI×開発組織 Summit 2026
techtekt
PRO
0
470
Featured
See All Featured
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
3k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
710
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.9k
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
380
The untapped power of vector embeddings
frankvandijk
2
1.9k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
203
77k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.6k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Paper Plane (Part 1)
katiecoart
PRO
2
12k
Color Theory Basics | Prateek | Gurzu
gurzu
1
490
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
Transcript
Rails Context Validation Rails Context Validation @kbaba1001 Powered by Rabbit
2.1.3 and COZMIXNG
自己紹介 @kbaba1001 永和システムマネジメント Ruby on Rails
ぼっち牧場
今日の話 Rails で状況によって変化するバリ デーションを作る。
なんで? ウィザードを作ったら画面ごとに 異なるバリデーションが必要にな った。
validates メソッド 状況によってバリデーションを変 えるためのオプション -> if、unless、on
if オプション validates :hoge, presence: true, if: -> { first_login?
} validates :fuga, presence: true, if: :fuga_true? def fuga_true? # ... end
with_options with_options if: -> { first_login? } do |a| a.validates
:hoge, presence: true a.validates :fuga, presence: true end
入れ子 with_options if: -> { first_login? } do |a| a.validates
:hoge, presence: true with_options if: -> { admin? } do |b| b.validates :foo, presence: true end end
入れ子 with_options if: -> { first_login? } do |a| a.validates
:hoge, presence: true # ここではfirst_login?が無効 with_options if: -> { admin? } do |b| b.validates :foo, presence: true end end
入れ子 with_options if: -> { first_login? } do |a| a.validates
:hoge, presence: true # first_login? も書く必要がある with_options if: -> { first_login? && admin? } do |b| b.validates :foo, presence: true end end
( ˘ω˘) 入れ子とはなんだったのか…
抜け道
unlessオプション with_options if: -> { first_login? } do |a| a.validates
:hoge, presence: true # if オプションを上書きしないで済む with_options unless: -> { !admin? } do |b| b.validates :foo, presence: true end end
(o´・∀・`) イエーイ
牧場行こ…
つらい 読みにくい 画面に依存しがち
onオプションを使おう
onオプション # Model validates :hoge, presence: true, on: :first_login validates
:fuga, presence: true, on: :create # Controller Model.save(context: :first_login) Model.create # on: :create のバリデーションが適用される
呼び出す側に条件を移す if first_login? Model.save(context: :first_login) if admin? Model.save(context: :admin) end
end
複数指定できない # Model validates :hoge, presence: true, on: :first_login validates
:fuga, presence: true, on: :create # Controller Model.create(context: :first_login) # これは無理 Model.save(context: [:first_login, :create]) # これも無理
複数のcontext if hoge.valid?(:first_login) && hoge.valid?(:create) hoge.save!(validate: false) end
複雑になると… with_options on: :create { ... } with_options on: :api
{ ... } with_options on: :web { ... } with_options on: :first_login { ... } with_options on: :withdrawn { ... } (´;ω;`)ウッ…
そもそも設計を見直す バリデーション用クラスを作る DBテーブルとモデルを分ける
まとめ if、unless 辛い on を使う 設計を見直す
マザー牧場へのアクセス 東京駅 (総武線) 君津駅 (内房線) 佐貫町駅 無料送迎バス Powered by Rabbit
2.1.3 and COZMIXNG