Check if the field value in the before_update filter has changed

I have a database field in which I want to save my password. In the before_create filter in my model, I call the encryption function and save from plain text to encrypted text.

I want to use before_update now also for encryption, but only if the value has changed. How to write a condition to check if a field value has changed?

+5
source share
2 answers

Since you usually don’t store the password in the model, using the field that you put in the form should be enough to update it unless password.blank?and have a real password in the hashed_password field that you won’t put in the form.

Thanks to Ben (see below) for telling me to additionally protect your encrypted password with attr_protected, so that it cannot be directly obtained / updated from the form. +1

+4
source

If the field is called a name, then

object.name_changed?

will return true.

+24
source

All Articles