Rails Validations

Alex Ngundji
3 min readFeb 15, 2021

Validations are used to ensure that only valid data is saved into your database. For example, it may be important to your application to ensure that every user provides a valid email address and mailing address.

There are several ways to validate data before it is saved into your database, including native database constraints, client-side validations, controller-level validations, and model-level validations:

  • Database constraints and/or stored procedures make the validation mechanisms database-dependent and can make testing and maintenance more difficult. However, if your database is used by other applications, it may be a good idea to use some constraints at the database level. Additionally, database-level validations can safely handle some things (such as uniqueness in heavily-used tables) that can be difficult to implement otherwise.

Confirmation

Useful for validating when two text fields need to have the same entry. Think of an email confirmation for example.

class User < ApplicationRecordvalidates :email, confirmation: trueend

Now in your view you can have two fields:

<%= text_field :user, :email %><%= text_field :user, :email_confirmation %>

One gotcha here is that this validation only performs if the presence option is set to true in the model as well.

So the User model gets an update:

class User < ApplicationRecordvalidates :email, confirmation: truevalidates :email_confirmation, presence: trueend

Exclusion

If you need to reserve any words or make sure a given entry is unique use exclusion.

class Profile < ApplicationRecordvalidates :username, exclusion: { in: %w(username1 username2 username3), message: “%{value} has already been taken.” }end

Notice how I used :message here as well. This is provided on all validation helpers.

You can also use inclusion in the opposite manner. See the docs for more info!

Format

Formatting strings are sometimes ideal for your app. You can use regular expressions within validations to alter whatever you’re after.

class Plan < ApplicationRecordvalidates :plan_type, format: { with: /\A[a-zA-Z]+\z/, message: “Plans can only inlude letters”}end

Length

I use this one all the time to put a constraint on the character length of a given field.

class User < ApplicationRecordvalidates :name, length: { minimum: 5 } # sets a minimumvalidates :bio, length: { maximum: 500 } # sets a maximumvalidates :handle, length: { in: 5..16 } # provide a rangeend

Feel free to personalize the messages with :message here as well. The following are all available to hook into to customize the message.

  • wrong_length
  • too_long
  • too_short

Example

class User < ApplicationRecordvalidates :bio, length: { maximum: 800, too_long: “%{count} characters is the maximum.” }end

Presence

This is probably the most widely used helper in my tool bag. Presence checks that fields are not empty.

class User < ApplicationRecordvalidates :name, :email, presence: trueend

You can also test an association exists on models

class Article < ApplicationRecordbelongs_to :uservalidates :user, presence: true # looks for a user_id field on the articleend

On the opposing model, you can do the same with one catch called inverse_of

class User < ApplicationRecordhas_many :articles, inverse_of: :userend

Uniqueness

Maybe you want unique usernames on your app? Uniqueness helps with that.

class User < ApplicationRecordvalidates :username, uniqueness: true, { message: “Username already taken” }end

--

--