0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【学習アウトプット3】has_secure_passwordとvalidates :password, presence: trueの違い

Last updated at Posted at 2019-04-16

背景

has_secure_passwordを設定したUserモデル

user.rb
class User < ApplicationRecord
  has_secure_password

が、以下のテスト

user_controller_test.rb
test "password should be exist when create" do
  @user.password = " "
  assert_not @user.valid?
end

で引っかかってしまう問題に悩んでいました。
(has_secure_passwordは空白にvalidationがかかると思っていたため https://qiita.com/Shantti-Y/items/19ea23b81f3421063fc5)

validates :password, presence: trueをつけるとテストをパスします。
じゃあ、has_secure_passwordのデフォルトvalidationとは一体...:rolling_eyes:

結論

Rails 4.2から以下のように変更されていたとのことです。

has_secure_password がデフォルトで空白のパスワードを許容するようになりました (例: 空白スペースのみのパスワード)。 https://railsguides.jp/4_2_release_notes.html

空白を許可したくない場合は別途validates :password, presence: true等の追加が必要みたいです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?