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
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
"優秀な誰か"の下位互換にならないために
yut0naga1_fa
1
1.8k
Claude Code本は、どのように執筆チームで書かれたか
oikon48
1
380
Railsで挑む大量データ永続化 〜600億件を永続化するための設計とボトルネックとの戦い〜
occhi
1
580
hackers_champloo_2026_tidb
makocchi
0
160
脆弱性診断って、何をしているの? 〜弱点を見つけて、会社の改善につなげるまで〜
keeg
0
920
Browser Spec: Testing Hotwire with Ruby
takahashim
1
120
Benchmarking Disaggregated Serving on vLLM + Dynamo with a Real Coding-Agent Workload
nttcom
1
190
自作バイナリを動かす「入口」8選
puhitaku
2
1.3k
M5Stackで作る BLEマウス GPSログの世界
tomokusaba
0
420
Gemini Intelligence 入門 ― AppFunctions と Gemini Nano
takuroabe
0
250
Rust で関数型ドメインモデリング
takonda
0
140
Getting Started with Google Antigravity
glaforge
0
300
Featured
See All Featured
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
30 Presentation Tips
portentint
PRO
1
430
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
480
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
So, you think you're a good person
axbom
PRO
2
2.2k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
1k
The SEO identity crisis: Don't let AI make you average
varn
0
590
StorybookのUI Testing Handbookを読んだ
zakiyama
31
7k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
2
2k
Testing 201, or: Great Expectations
jmmastey
46
8.3k
Side Projects
sachag
456
43k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.2k
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