Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
041 Conditional Validations
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