Skip to content

Instantly share code, notes, and snippets.

@tatat
Last active December 16, 2015 21:09
Show Gist options
  • Save tatat/5497457 to your computer and use it in GitHub Desktop.
Save tatat/5497457 to your computer and use it in GitHub Desktop.
Rails4(くらい)で has_secure_password のバリデーションをスキップしたり
class User < ActiveRecord::Base
has_secure_password validations: false
validates :password,
presence: {if: :password_required?, on: :create},
confirmation: {if: :password_required?}
validates :password_confirmation,
presence: {if: :password_present?}
before_create {
raise 'Password digest missing on new record' if
password_required? and password_digest.blank?
}
def password_present?
password.present?
end
def password_required?
raise 'ここに条件が来ます。'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment