I am a Sr. Software Developer at Oracle Cloud. The opinions expressed here are my own and not necessarily those of my employer.
Sep 29, 2016
Rails model validations are very important for ensuring data integrity. You usually start with really simple inlinevalidates :name, presence: true.
You can then implement more complex validations for inclusion, format, numericallity. Or add conditional validations. Eventually the logic gets too complex for one line statements and you write custom methods
But then you encounter a situation where you need to perform the same validations in two different models.
However, you will get these warnings in your logs:
Plus this approach is just not very clean. Instead, why not create a app/validators folder and put the class there?
Keep in mind that these validators are POROs so you could create class AppplicationValidator < ActiveModel::Validator to contain common logic and inherit from that. And you can create spec/validators/name_validator_spec.rb and test validation by passing different record types (user or organization).
One useful place for custom validator class is when records can only belong to certain types of records. Let’s say we have Account that belongs_to Organization. User also belongs_to Organization AND has_and_belongs_to_many Accounts. But User cannot have Account that belongs_to different Organization than User.