Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Has_secure_password `validate :password, if: :present?` not working on an existing record
# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.1.1'
require 'sqlite3'
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :users do |t|
t.string :password_digest
end
end
class User < ActiveRecord::Base
has_secure_password
validates :password, length: { minimum: 8 }, if: :present?
end
class HasSecurePasswordPasswordValidationBugTest < Minitest::Test
def test_has_secure_password_validates_if_present_correctly
user = User.create!(password: "password")
assert user.valid? # passes
assert user.reload.valid? # passes
user = User.last
assert user.valid? # fails
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment