Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails model validation on create and update only

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?

like image 416
Jakub Arnold Avatar asked Sep 08 '09 12:09

Jakub Arnold


2 Answers

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.

like image 74
Alessandra Pereyra Avatar answered Sep 24 '22 00:09

Alessandra Pereyra


Only write:

validates_presence_of :password 

No need...

on => :create 
like image 22
FJ. Avatar answered Sep 21 '22 00:09

FJ.