041 Conditional Validations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
validates_presence_of :password, if: :should_validate_password? | |
validates_presence_of :country | |
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_passsword = true | |
@user.save | |
@user.save(false) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment