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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
560
Lume: Static Site Generator
kbaba1001
0
740
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
おい、エージェントを使って終わらせろ
nwiizo
3
870
2026-09-18 gotanda.sre Terraformで複数環境作ったり、複数Stateに分割したりそれとTerragrunt / Terraform multi envs and multi states
masasuzu
4
700
手を動かして実感する、Kiro が変える開発体験
inariku
0
190
品質と信頼性を地続きにする
grimoh
2
950
Deployment の 先にある AI Agent 基盤 - kagent vNext、Agent Substrate、Hermes から読み解く Agent Runtime の現在地 / k8s-matsuri-2-ai-agent-platform-amsy810
masayaaoyama
4
710
「ピッケル本」日本語版は4.0(第6版)が出版されるべき / pickaxe4-nagoyark05
kakutani
2
280
あけおめLINE 傾向とその対策
nasa9084
0
300
AIエージェントの権限管理 3: Agentic RAG の Fine grained access control 編
ren8k
0
210
アクセスキーこわい やめかたと漏らさない工夫
sassssan68
1
650
俺の仕事は AIに奪われないし、たぶんその BIも要らない
hikaruri
0
540
Kiro Meetup #8 Kiro アップデート (2026/3/21〜2026/9/24)
katzueno
1
150
Antigravity SDK for the Java Developer
glaforge
0
180
Featured
See All Featured
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.9k
Building Flexible Design Systems
yeseniaperezcruz
330
41k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.6k
New Earth Scene 8
popppiees
4
2.6k
Marketing to machines
jonoalderson
1
5.8k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
69
66k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
580
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
560
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
440
The agentic SEO stack - context over prompts
schlessera
0
940
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
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