Back to Validations

38 views
Skip to first unread message

bee...@gmail.com

unread,
Nov 7, 2023, 2:40:12 PM11/7/23
to sinatrarb
I have a password field that I want to validate as the entered password.  The validate{} method, I use for this using valid?.  I want to use a before_save or before_create to give instructions to call encrypt_password() before Object.save.  

It seems neither of those two are available and I want to know how to accomplish this.  Do these methods exist?  before_create and before_update seem useful.  

bee...@gmail.com

unread,
Nov 7, 2023, 3:42:28 PM11/7/23
to sinatrarb
Found it.  hook_class_methods.  

bee...@gmail.com

unread,
Nov 22, 2023, 7:02:53 PM11/22/23
to sinatrarb
I've moved away from before_creat (not recommended anyway) and my own methods are finding something I can't explain.

user.pre_validation # sets and changes some ivars
if user.valid? # unencrypted password, returns true
user.post_validation # encrypt password
saved_user = user.save # somehow validation happens again, throws validation error

Is there another call in user.save for validation?  I thought I had already validated the object.  Validation on the password field has a range smaller than the encrypted length.  So I validate on the submitted version, and want to encrypt post validation as a before_create.  But it won't progress because it fails a second validation that isn't in the route.  

bee...@gmail.com

unread,
Nov 23, 2023, 9:24:42 PM11/23/23
to sinatrarb
Use user.create instead.  
Reply all
Reply to author
Forward
0 new messages