A quick look at Rails Custom Validation

Raymond Blessed
May 8, 2019 · 4 min read
Image for post
Image for post

I recently started working with Ruby (almost 2 months now) and Ruby on Rails (a little over 3 weeks). Working with Rails’ Active Record framework is one of my favorite things about Ruby on Rails. In this post, we will be looking at validations in Active Record, custom ones particularly. Here is a quick intro to Active Record before we move to the good stuff.

Active Record is one of the core gems that make up Ruby on Rails. It is the part of the framework that deals with databases.

It is an ORM (Object Relational Mapping) framework that lets you build a schema for a database using pure ruby and it is based on the Active Record design pattern described by Martin Fowler. So, with Active Records, you are creating your DB, creating tables, storing, retrieving and deleting data using ruby which translates to SQL under the hood.

Quick Intro

To interact with the database, we use methods inherited from the ApplicationRecord superclass.

It also supports associations and other database stuff.

For a detailed intro to Active Record, check out the official ruby on rails guide.


Let’s look at our students table above. We wouldn’t want to create a user without a first name or last name which presently is possible. To mitigate this, we just need to modify our Student class like so:

With this modification, when you create a Student instance without the first name or last name attributes, it is an invalid student and active records will not persist it to the database.

Active record also provides us with methods to check if our data is valid or invalid:

With this, we do not even have to attempt to save the data.

Apart from just preventing the data from being persisted, Active Record also provides an error list that holds the attributes that failed validations and user-friendly messages to present to the users. These errors can be accessed as shown in the snippet below.

There is a lot more on validation but it’s not the topic of this article. For a deep dive, you can get an in-depth explanation from the ruby on rails guide chapter on Validation.

Custom Validation

So, let’s say for our Student model, we have a compulsory student registration number column. It has to be filled from the registration form (I know this can be auto-generated) which should always start with the registration year. Now, Active Record does not provide this kind of validation out of the box, but has made it possible for us to define it and use it.

There are mainly two ways to define your own validation logic:

  • Custom Validator
  • Custom Methods

Custom Validator

If validation fails, it adds the attribute to the errors array along with its error message. So, in our case, we’ll have RegNumValidator as seen below:

To use this validator in the Student model, we use the validates_with method:

With this, when a user tries to create a student with the wrong registration number, the record creation fails and an error message can be shown.

Custom Methods



This is no longer updated.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store