I have seen quite a few threads about where the best place is to put your custom validation classes (extending ActiveModel::EachValidator), but can't figure out which one is best practice. Some threads and tutorials say put them in 'lib/' and modify '/config/application.rb', other threads say if you just put them in 'app/validators' they will be automatically included... any ideas on this? What is the best practice for this?
The 'libs/' solution has a downside because you have to modify '/config/application.rb', but as far as I understand, placing it in 'app/validators' will include it, although you have to add a directory to the app folder. Is this right? Thanks!
Yes, when you place your custom validator into "app/validators", it will be automatically included. When you use directory "lib" or "lib/validators", you will have to place this line into config/application.rb file:
config.autoload_paths += %W(#{config.root}/lib)
or
config.autoload_paths += %W(#{config.root}/lib/validators)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With