Search code examples
ruby-on-railsvalidationmodel

Does rails custom validation fail on return true?


According to this answer, the custom validation fails when the method return true.

I want to know if this is mentioned in official docs? The rails guide only mention errors.add as a way to trigger custom validation method to fail.


Solution

  • It depends how you are using your custom validations.

    If you are using Active Record Callbacks, i.e. before_save or before_validation, if you return false from any of these, your validation will FAIL and the record will not be saved: http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html#module-ActiveRecord::Callbacks-label-before_validation-2A+returning+statements

    However if you are using a custom validator such as mentioned here: http://guides.rubyonrails.org/active_record_validations.html#performing-custom-validations the return value is not significant (It is not mentioned in the docs, as you say), what matters is if you add to the errors array.