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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
kbaba1001
December 08, 2014
Technology
810
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
80
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
220
fast optical line
kbaba1001
0
420
ArtPosePro and Procreate
kbaba1001
1
260
Other Decks in Technology
See All in Technology
LLM Internals: 언어 모델의 계보와 알고리즘 진화 (2023~2026)
inureyes
PRO
1
880
案件に一番詳しいAIを Amazon Bedrock AgentCore で作る ― 知見が知見を生むチームへ / Compounding Knowledge with AgentCore
yusukeshimizu
1
170
AIで変わるエンジニアの働き方(仮)
naoinaoi
0
390
tamachi.go 誕生の裏側
rymiyamoto
1
200
AI時代のアウトプット――変わったこと、変わらないこと / Devsumi 2026 Kansai #devsumi
jnchito
0
500
AIに狂うスタートアップが、あえて「人との協働」に全振りした新卒エンジニア研修 / New Graduate Engineer Training at a Startup Accelerating AI Adoption
ohnoeight
0
180
FORENSIA: ローカルLLMフォレンジックハーネス
sumeshi
2
440
国家プロジェクトを支える「さくらONE」 大規模LLM開発におけるGPU障害を乗り越えるクラスター運用戦略
gpuunite_official
0
250
アクセスキーが漏れた日にやるべきこと- 無効化の先にある本当の対応
kazzpapa3
0
200
AI時代に、人は何を、どう学ぶのか #pbl_pub / What and How Do We Learn in the AI Era?
takaking22
1
240
[potatotips #96] Give Your AI Agent the Flutter Playbook
korodroid
0
140
도구에서 동료까지: 10년차 AI 스타트업의 AI 적응기
inureyes
PRO
1
330
Featured
See All Featured
How to Ace a Technical Interview
jacobian
281
24k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
280
Git: the NoSQL Database
bkeepers
PRO
432
67k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
500
Become a Pro
speakerdeck
PRO
31
6.2k
So, you think you're a good person
axbom
PRO
2
2.1k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
56
3.4k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
490
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.1k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
201
75k
Everyday Curiosity
cassininazir
0
290
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