class User < ActiveRecord::Base validates_presence_of :password, :if => :should_validate_password? validates_presence_of :state, :on => :create validates_presence_of :state, :if => :in_us? attr_accessor :updating_password def in_us? country == 'US' end def should_validate_password? updating_password || new_record? end end # in controller @user.updating_password = true @user.save @user.save(false) # will avoid any validation in the model
Wednesday, April 2, 2008
Conditional validations
from here
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment