botbot
botbot

Reputation: 7359

Rails 3: Where is the best place to place custom validator classes?

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!

Upvotes: 6

Views: 1440

Answers (1)

bender
bender

Reputation: 1428

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)

Upvotes: 4

Related Questions