Rohan Daxini

Joined

1,410 Experience
14 Lessons Completed
0 Questions Solved

Activity

Posted in How to Build Custom ActiveRecord Validations Discussion

Lee McAlilly,

Yes, that's correct. Refer to this "Active Model Validator API" here https://edgeapi.rubyonrails.org/classes/ActiveModel/Validator.html

While adding our own custom validation for an attribute, we need to create a subclass of ActiveModel::EachValidator and implement the "validate_each" method.

Once we create a new class, for example above "ResizableImageValidator" with the file name as resizable_image_validator.rb, the Rails fw convention will take care of adding this as a custom validator in the validation chain. Generally, when files are kept in the Rails "app" folder, it will be autoloader, and hence that custom validator is also available on app load. You may also refer to this discussion here https://gorails.com/forum/where-do-i-put-a-custom-validator-for-a-model