Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the4dpatrick/9629526 to your computer and use it in GitHub Desktop.
Save the4dpatrick/9629526 to your computer and use it in GitHub Desktop.
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