/ Published in: Rails
From railscast #41
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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