# Conditional before\_safe

**URL:** <https://www.ruby-forum.com/t/conditional-before-safe/118485>\
**Category:** Rails\
**Created:** [October 27, 2007, 6:15pm UTC](https://www.ruby-forum.com/t/conditional-before-safe/118485 "2007-10-27T18:15:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [October 27, 2007, 6:15pm UTC](https://www.ruby-forum.com/t/conditional-before-safe/118485/1 "2007-10-27T18:15:40Z")

</div>

How to run before\_safe only if a database field contains a certain  
value?

I tried:

before\_save { |guest| guest.password = md5\_pass(guest.password) ,  
:if =\> :enabled? }

def enabled?  
self.deleted == 1  
end

No success!

–  
Jochen K.  
[railswerk.de](http://railswerk.de), [gissmoh.de](http://gissmoh.de), [figgfrosch.de](http://figgfrosch.de), [ror-ror.de](http://ror-ror.de)

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [October 27, 2007, 6:27pm UTC](https://www.ruby-forum.com/t/conditional-before-safe/118485/2 "2007-10-27T18:27:21Z")

</div>

That shouldn’t work. I think you are getting before\_save confused with  
built-in validations. Try it like this:

before\_save { |guest| guest.password = md5\_pass(guest.password) if  
guest.enabled? }

---

<div class="post-metadata">

**Author:** ![jawosis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f04885/32.png) [@jawosis](https://www.ruby-forum.com/u/jawosis)\
**Post date:** [October 27, 2007, 6:38pm UTC](https://www.ruby-forum.com/t/conditional-before-safe/118485/3 "2007-10-27T18:38:27Z")

</div>

William P. schrieb:

> That shouldn’t work. I think you are getting before\_save confused with  
> built-in validations. Try it like this:
> 
> before\_save { |guest| guest.password = md5\_pass(guest.password) if guest.enabled? }

Ok, that’s it! Thanx.

–  
Jochen K.  
[railswerk.de](http://railswerk.de), [gissmoh.de](http://gissmoh.de), [figgfrosch.de](http://figgfrosch.de), [ror-ror.de](http://ror-ror.de)
