From the course: Ruby on Rails 6 Essential Training: Models and Associations

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Write custom validations

Write custom validations - Ruby on Rails Tutorial

From the course: Ruby on Rails 6 Essential Training: Models and Associations

Start my 1-month free trial

Write custom validations

- [Instructor] Most of the time, the built-in validations that Ruby on Rails gives you are going to be enough to help you keep your data under control. But every now and then there's some special cases that come up where they're not enough. and we'd like to be able to write our own custom validations. Well, Ruby on Rails lets us do that too. The syntax port is the validate method not validates plural which is what we saw in the last movie. This is validate singular. And afterward validate we provide a symbol that is the name of a method that we want it to run for its validation. Here I've just called it custom method. The method that you write has two key parts, the test case and adding errors. Every validation method ultimately has to have a test case. It could be a very simple test case, just checking to see if an attribute has a certain property or it could be very complex. You might go to other database tables and…

Contents