DEV Community

andrewjpwalters
andrewjpwalters

Posted on

Active Record Validations

One of the key features of Ruby on Rails is its powerful Active Record ORM (Object Relational Mapping) layer, which abstracts away the details of database access and allows you to work with your data in a more object-oriented way. One of the most important features of Active Record is its support for validations, which allow you to ensure that the data stored in your database is always consistent, accurate, and valid.

What are Active Record Validations?

Active Record validations live on the model layer of Rails, which is structured on the MVC architecture. They are a way to check that the data stored in your database is always valid and consistent. They allow you to define rules or constraints on the data, and then check that those rules are being followed whenever you try to save or update an object to the database. For example, you might want to ensure that a user's email address is unique, or that a blog post's title is at least 5 characters long.

Active Record provides a wide range of built-in validation helpers that make it easy to define these rules. Some of the most common validation helpers include:

  • presence: ensures that a field is not empty or nil
  • uniqueness: ensures that a field is unique across all records in the table
  • numericality: ensures that a field is a number
  • length: ensures that a field is a certain length
  • format: ensures that a field matches a certain regular expression

When are Active Record Validations Run?

Active Record validations are run automatically whenever you try to save or update an object to the database using the save, create or update methods. When you call one of these methods, Active Record first checks that all the validations defined for the object pass. If any of the validations fail, the operation is cancelled and the object is not saved to the database.

For example, let's say you have a User model with a name field that must be present. You might define a validation like this:

class User < ApplicationRecord
  validates :name, presence: true
end
Enter fullscreen mode Exit fullscreen mode

Now, if you try to save a User object without a name, like this:

user = User.new
user.save!
Enter fullscreen mode Exit fullscreen mode

The save method will return false, indicating that the object was not saved, and, by using the bang version of save, an exception will be raised and the errors object on the User object will contain an error message indicating that the name field must be present. The same is true for the bang version of create and update.

In addition to running validations when you save an object, you can also manually trigger validations on an object by calling the valid? method. This method runs all the validations for the object and returns true if all validations pass, or false if any of them fail.

For example:

User.create(name: "Jane Johnson” ).valid?
=> true
User.create(name: nil).valid?
=> false
Enter fullscreen mode Exit fullscreen mode

In short, if you're new to Ruby on Rails like I am, learning how to use Active Record validations is essential. By mastering this feature, you'll be able to build more robust, secure, and user-friendly applications that store consistent and valid data.

Top comments (1)

Collapse
 
samuelfaure profile image
Samuel-Zacharie FAURE

Image description

Publishing AI-generated content without disclosing it is against Dev.to guidelines