From the course: Ruby on Rails 5 Essential Training
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Custom validations - Ruby on Rails Tutorial
From the course: Ruby on Rails 5 Essential Training
Custom validations
- [Narrator] I think you'll find that the built-in set of active record validations will meet most of your needs, most of the time. However it's also possible to write custom validations if they don't meet your needs. It's actually very easy to do. All you need to do is use the validate method, followed by the name of another method, which should be run for your validations. So here I have validate custom_method. Somewhere else in my model then, I need to define that custom method and that custom method is going to determine whether or not this object is valid. Typically you want to make this method a private method. That means that you would put it below a line that says private. Anything following private to be considered private method and that means that it won't be accessible outside of this class. No person or code will be able to get to it except for the current model. The reason that validations fail is because errors are present in the errors array. That's all that valid…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.