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
Rails Context Validation
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
81
Build React system with ClojureScript (Squint)
kbaba1001
0
200
talk-with-local-llm-with-web-streams-api
kbaba1001
0
550
Lume: Static Site Generator
kbaba1001
0
730
React_2023
kbaba1001
0
210
Word Penne
kbaba1001
0
250
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
最新技術に積極チャレンジ!EKS共通基盤のこれまでとこれから
daitak
0
290
tamachi.goを支える技術
rymiyamoto
0
130
[RSJ26] Hierarchy-Aware Multimodal Retrieval-Augmented Generation for Embodied Question Answering
keio_smilab
PRO
1
150
Level Up Your CDK DX: 5 Tools I’ve Been Building
gotok365
2
170
Master Dataグループ紹介資料
sansan33
PRO
1
4.9k
Genie Code ワークショップ 応用編 / Genie-Code-Workshop-advanced
databricksjapan
PRO
0
250
全社に広がるMCPサーバーを、 どう安全に管理するか MCPass開発の舞台裏
mtpooh
3
240
Point Cloud as a Foreign Language for Multi-modal Large Language Model
takmin
0
360
Driving AI Adoption Using In-House GPUs to Serve Qwen
po3rin
1
290
All About Sansan – for New Global Engineers
sansan33
PRO
1
1.5k
AI時代に、プロダクトの数だけ積み上がる所有コストをどうエンジニアリングするか / Engineering the Cost of Ownership
kzkmaeda
0
110
Kiro5兄弟のいまどきのセキュリティ基礎知識
kentapapa
0
490
Featured
See All Featured
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
6.1k
How to Talk to Developers About Accessibility
jct
2
530
Optimising Largest Contentful Paint
csswizardry
37
3.9k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
2
3.8k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
120
120k
Discover your Explorer Soul
emna__ayadi
2
1.3k
A better future with KSS
kneath
240
18k
Code Review Best Practice
trishagee
74
20k
Writing Fast Ruby
sferik
630
63k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
260
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.7k
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