Conditional before_save :encrypt_password

I am having a problem finding the best way to make a “before_save
:encrypt_password” conditional.

I have to at times update user model attributes but each time I do this
the password is reencrypted because of the above. I need to
differentiate between when the user is first logging in and the password
does need to be encrypted, and when they are already logged in and the
“before_save :encrypt_password” should not be called.

eg
if !signed_in?
before_save :encrypt_password
end

This does not work but Is there a rails variable that gets set when
logged in I can use?

Thanks,

DC

I am having a problem finding the best way to make a “before_save
:encrypt_password” conditional.

I have to at times update user model attributes but each time I do this
the password is reencrypted because of the above. I need to
differentiate between when the user is first logging in and the password
does need to be encrypted, and when they are already logged in and the
“before_save :encrypt_password” should not be called.

eg
if !signed_in?
before_save :encrypt_password
end

This does not work but Is there a rails variable that gets set when
logged in I can use?

Thanks,

DC

Sorry,
Should be posted in rails forum…

You got the wrong Maillinglist, it’s Ruby, not RoR, latter got it’s own
ML, check google for the address…

Anyway, try:

before_save :encrypt_password, :if => :is_new_user?

private
def is_new_user
new_record?
end

Of course, adjust the :is_new_user method as needed.

Greets, Chris

PS. You can also use procs instead symbols on the :if condition, useful
for simple 1 liners
:if => lambda{ new_record? }