Basic Validations in Ruby On Rails

T. J. Marstiller
4 min readJan 14, 2021

A short and handy guide on how to use common basic validations in rails.

Validations are used to ensure that only valid data is saved into your database… ~ (rubyonrails.org Rails Guides)

Validations are inserted directly into your model as show below and I am going to show you a few basic validations that you can use and a few examples of how they work. Below I made a typical “User” model for demonstration.

The first example is “presence” which makes sure that there are no nil values in a form such as a new.html.erb view form to create a new user.

Presence

We want to make sure that each field is filled in by a user so we “validates” each attribute in the model and set its “presence” to “true” as shown below.

So now when a user doesn’t fill in the required information to create a new user they will not be able to create a new user without making sure to fill out the entire form with some and/or correct data.

Uniqueness

The next validation is “uniqueness”. It is used to make sure in this case, the users email is unique before it gets saved into the database. There is a caveat though and I’m going to quote the rubyonrails.org Rails Guides below.

“This helper validates that the attribute’s value is unique right before the object gets saved. It does not create a uniqueness constraint in the database, so it may happen that two different database connections create two records with the same value for a column that you intend to be unique. To avoid that, you must create a unique index on that column in your database.”

Confirmation

The next validation is for email confirmation. This is used when you want someone to confirm their information such as an email or password and is appended with “_confirmation” in both the “presence” as well the view template as shown below.

This will produce the error for not confirming email as show below.

Length

The next and last basic validation I’ll show is a validation for the “length” of the information that a user must input into their form to create a new user. This will ensure that the minimum amount of characters is input into a text field.

Error Messages

So how do you get the error messages to show on the page you may ask?

One last thing, the validations will work but will not show the errors to the users automatically and the “User” may not know why… So to get the errors to show we will use “flash” to allow the errors to show up on the page when a user does not input their necessary information into the form fields.

In the “users_controller.rb” when we define the create action we create a conditional such as below… (note the user_params which is another subject). This will check if the user information is “valid?” and if it is, which means all fields meet our validations, it will save the new user into the ActiveRecord database “else” if it’s not true it will use “flash” the “errors” and show all messages to the user as they are redirected back to the create a new user form.

For these messages to show up to the user on the form we need to add something more to the top of the new.html.erb file. We add it to the top because we as humans typically read from “top down”, so it makes sense to have the messages show at the top of the form.

I hope you enjoyed this tutorial on basic Ruby on Rails Validations!

For more information on Rails Active Record Validations check out https://guides.rubyonrails.org/active_record_validations.html

--

--