views:

39

answers:

1

I have this in my user.rb:

 attr_accessor :old_password 
  def validate
    unless password.nil?
      errors.add_to_base("Old password entered incorrect")  unless self.valid_password? old_password 
    end
   end

I have old_password as a a virtual attribute that has to be validated as matching with the current before updating to a new password. My problem is that upon correct entering ( password == password confirmation and self.valid_password? old_password ) an error will yield and pass me back to the form. The strange part is that the data will actually be updated in the database, and it will not on wrong input; although it will yield the very same error ("Old password entered incorrect"). What on earth am I doing wrong?

A: 

Alright, found the problem. I used a custom update attributes, which used to look like:

def custom_update_attributes(userparams, updater)
     unless updater.may_change_user_role_name? self
       userparams.delete(:role_name)
       userparams.delete(:active)
     end
     self.update_attributes(userparams)
     save
   end

The obvious problem here is save, even though I changed my def validate to def validate_on_update this validation has been running on the save action too, and since no old_password is supplied, error will be yielded.

Fredrik