51

If I want to have validation only on create, then I can do

validates_presence_of :password, :on => :create

But how do I say on create and update? I tried this but it didn't work:

validates_presence_of :password, :on => [ :create, :update ]

Do I have to define the validation two times?

2
  • 4
    Could be a patch in the making... except how is :create and :update different from the default :save? (Wouldn't :save be both?) Commented Sep 8, 2009 at 12:44
  • 1
    yep, I just found out that :save is both, thx Commented Sep 8, 2009 at 22:42

3 Answers 3

66

By default, the validations run for both create and update. So it should be just:

validates_presence_of :password

The :on key just allows you to choose one of them.

Sign up to request clarification or add additional context in comments.

1 Comment

the new convention is actually to use: validates :password, presence: true
12

Only write:

validates_presence_of :password

No need...

on => :create

Comments

0

You can use this when you need to disable the validation on some specific operations, such as delete.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.