Creating Active Record Custom Validators
As an experienced Rails developer, you’re probably familiar with Active Record validations.[92] Active Record provides a huge list of validations, and many of them can be combined together.
This system is extensible, meaning developers can create their own validations as custom validators. Custom validators[93] are created using object oriented design concepts we’ll explore next.
How are they set up? To create one, create a Ruby class that extends the ActiveModel::Validator class and implements a validate() instance method. The .validate method accepts a record argument. When those two ingredients are in place, you’ve created a custom validator.
Individual attributes for a model can be validated by ...
Get High Performance PostgreSQL for Rails now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.