robodisco
robodisco

Reputation: 4272

Rails - How to set a validation to occur in just one of my own controller methods

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?

Upvotes: 0

Views: 84

Answers (2)

khelll
khelll

Reputation: 23990

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.

Upvotes: 2

marcgg
marcgg

Reputation: 66436

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...

Upvotes: 2

Related Questions