Rails model validation on create and update only

Jakub Arnold picture Jakub Arnold · Sep 8, 2009 · Viewed 48.7k times · Source

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?

Answer

Alessandra Pereyra picture Alessandra Pereyra · Sep 8, 2009

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.