Where should Rails 3 custom validators be stored?

狂风中的少年 提交于 2019-11-27 18:14:08

If you place your custom validators in app/validators they will be automatically loaded without needing to alter your config/application.rb file.

gunit888

If you add this to your /config/application.rb file:

config.autoload_paths += %W["#{config.root}/lib/validators/"]

Then Rails will automatically load your validators on start up (just like /config/initializers/), but you keep the clean structure of having your validators in one nice, well named spot.

lib/validators seems by far the cleanest. However you may need to load them in before your models, so probably from an initializer.

Shreyas

Here's the official docs about custom validations. AFAIK its a good practice to keep them in the relevant models.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!