views:

282

answers:

2

Hi

I have a database field where I want to store my password. In a before_create filter in my model I call a encryption function and save from clear text to encrypted text.

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

+2  A: 

If the field is called name then

object.name_changed?

will return true.

Farrel
Won't this always return true? Rails will be comparing the updated plaintext password with the encrypted existing password in the database and they'll always be different (I think).
John Topley
A: 

Since you usually do not store the password in the model which you would expose to the form, it should be sufficient to only update it unless password.blank? and have the real password in a field "hashed_password" which you won't expose to the form.

hurikhan77