loader image
Ruby on Rails Web Development

How To Validate Ruby Objects With Active Model Validations


Why We Need Validations First?

With the help of validations you can ensure that only the valid data is saved into your database. For example, when your application demands unique email address for each users then we’re in a situation to collect only the valid data. Now you can achieve this with the help of Model level Validations in Rails. Rails provides several built-in helpers which will cover most of the validation part, Rails will also allows you to create your own custom validation methods. 

Types of Validations?

There are many ways to validate your data before saving into your database and here i will list down the 4 main ways to validate the data in Rails.

  • Database Constraints – We can create validations at database level, but it is very difficult to manage validations at database level, because for every modification of validation you need to run the migration again. So maintenance will become more difficult.


  • Client side Validations – Client  side validations are performed by browser, we will write these validations in JavaScript/Jquery, however we cannot use these validations alone. Because there might be a chance for Javascript is turned off in user browser, but these validation is very useful to give the immediate feedback to the user about what exactly went wrong. It is highly recommended to use both client and server side validations.


  • Controller-level Validations – We can add validations in your Rails Controller, but it is very difficult to test and maintain, Rails always will recommend us to keep the controller very light so that it will not add more code to your controller.


  • Model Level ValidationsRails always recommends to keep your validations at model level, this makes validations easy to test and maintain. In the coming section you could see how easy is that to add and test validations in Rails.


Validation Helpers  in Rails?


  • Presence:

This Validator will not allow the record to save in DB unless specified attributes are present.

Example 1:


Following example will throw error on empty name, email, age


Related: Authenticating Rails Web Services With JWT



This is the vice versa of the presence helper, this helper will always ensure that specified attribute must be empty.


Following example throws error on the presence of email.



This helper always checks for the attribute uniqueness before record gets saved into the database.


In First example I created user with email testuser@gmail.com, in 2nd example when I try to create the user with same email, it throws error.




This helper will save the record into database only if the attribute value is numeric. You can also pass different constraints like :greater_than, :greater_than_or_equal_to, :equal_to, :less_than, :less_than_or_equal_to, :odd, :even, :other_than all the constraints are self explanatory.


In this example, I tried to create the user age with string which was expected to be number, so it throws error

Following examples demonstrates how to use greater_than, less_than, other_than options with numericality helper.

This helper will validate the length of the attribute value and you can pass many constraints like minimum, maximum, in, is. “In” constraint for specifying length with in a range(which includes min and max values).”is” constraint for exact value length.

Following example demonstrates how to use length helper with different options like minimum, maximum, is, in.


Best To Read: How To Build Rock Solid Ruby On Rails App With BDD


This helper take a set of values and ensures that attribute value must match with any one of the value from the set of predefined values.




This is the total vice versa of inclusion helper, it will take a set of predefined values and it will ensure that attribute value must not match with any of the value in the list.




This helper take input as a regular expression and ensures that attribute value must match with regular expression.




This helper is useful when you have text boxes in your form which expects the same values, for example email, email_confirmation, password, password_confirmation. This helper will creates a virtual attribute for you, you need to add  _confirmation to the field.




This helper will always ensure that the checkbox in the form is checked when your form is submitted, one of the use case of this validation is, when the user is about to sign up then he/she must accept the term and conditions. Also, you don’t need to have any field in your database for this, rails will create a virtual attribute for you.




There are some cases where you need to validate the attribute only if the attribute value is present, else we need to skip the validation. In that case allow_nil option skips the validation if the attribute value is nil and it will do the validation only when the attribute value present.

First example will save the record if age attribute value is nil, second example will return errors if age value is not  in the specified list.



This option is almost similar to the allow_nil but only difference is “allow_nil” will accept the skip validation only when the value is nil where as allow_blank  will skip the validation if the attribute value is nil, or empty string (“ ”)


Allow blank saves the record although age value is empty string,



By default each validation helper has the default error message set by the rails, this message option will allow you to customize the error messages for you. 


The default validation error message of presence helper is “Name can’t be blank”(consider attribute name is “name”).

In the same way, you can edit any validation helper error message.


The default behavior of each validation helper is, they will run the validation before record gets stored into database i.e either while creating the record or while updating it. Also, this on option allow you to specify when the validation should happen.


Alright! Mostly we have seen covered all the major validations in Rails which can greatly help us in building user-centric forms. Also would appreciate your great suggestions to emphasize Rails validations. Concerned to build user centric site in Rails? Looking for the correct to take up? Get in touch with the highly skilled experts from Rails application development to guide you on building the user-friendly rails application.

The following two tabs change content below.

Venkata Krishna

An ideal full stack software developer having 2 years of experience in Web app development and expertise in Ruby on Rails, CMS services. Let it be a technology or charity he never fails to feed the needy. An honest individual who continuously involves himself in creating Social Awareness as part of his life goals.

Leave a Reply

Your email address will not be published. Required fields are marked *

Join our 30,000+ subscribers, never miss out anything on our latest blogs, tips, tutorials, updates & more.