views:

32

answers:

2

I have a password_reset_token field in my model and it just stores an authentication token which will be used to parse a reset password url.

It's going to be nil unless the forgot_password method in my controller is called. It's here the token is generated.

I want a validation to only run here otherwise every time I update a user object validations will complain about the empty token field. How do I do that?

+2  A: 

You can skip validation by doing so:

@my_object.save(false)

Of course, use it wisely. Skipping validation can end up with corrupted data being inserted in the database...

marcgg
+2  A: 

And why do you want to validate it? When the user goes to the forget password form, he will just have to submit his email address I suppose, and it's your turn to generate the authentication token, so basically there is no need to validate it as it won't be an input.

khelll
i see you point there but I thought validations could be used to not only the users typos but also any of the programmers in their code. Im new to rails / programming so if this is the wrong thing to do please tell me.
adam
There is no need to do it here. Validations are mainly for user submitted forms. Tests(TDD or BDD) are for developers to test that their code doesn't break specifications.
khelll