Skip to content

Instantly share code, notes, and snippets.

@certainty
Created July 25, 2012 13:26
Show Gist options
  • Save certainty/3176188 to your computer and use it in GitHub Desktop.
Save certainty/3176188 to your computer and use it in GitHub Desktop.
Example
class User < ActiveRecord::Base
validates :phone_number, presence: true, length: { is: 11 }, if: :gsm_number_empty?, unless: :password_or_email_changed?
validates :gsm_number, presence: true, length: { is: 11 }, if: :phone_number_empty?, unless: :password_or_email_changed?
private
def password_or_email_changed?
encrypted_password_changed? || email_changed?
end
def phone_number_empty?
!phone_number.present?
end
def gsm_number_empty?
!gsm_number.present?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment