I am following this solution: Rails 4 - Allow password change only if current password is correct
But my password updates regardless if I input the correct current password or not. Here is my code:
Employee model:
class Employee < ActiveRecord::Base
attr_accessor :password, :current_password
def self.authenticate(user, password)
employee = find_by_code(user)
if employee && employee.password_hash == BCrypt::Engine.hash_secret(password, employee.password_salt)
employee
end
end
validates_presence_of :current_password, if: :validate_password?, on: :update
validate :current_password_is_correct, if: :validate_password?, on: :update
def current_password_is_correct
if Employee.authenticate(code, current_password) == false
errors.add(:current_password, "Wrong password.")
end
end
def validate_password?
!password.blank?
end
end
If I change the current_password_is_correct to this it properly shows the error:
def current_password_is_correct
if Employee.authenticate(code, current_password) == false || true
errors.add(:current_password, "Wrong password.")
end
end
Which makes me think that probably the password is updated before this validation is executed. How can I be sure of this, and if it is so, how can I make it execute in the correct order?
Thanks