Rails 7.1: Normalizes - A powerful new feature for data validation

In Rails 7.1, a new method has been added to the Active Record called normalizes. This method allows developers to normalize values before they are persisted in the database.

What is normalization?

Normalization is the process of converting a value to a format that is consistent with the format required by the database. For example, a string can be normalized to a numeric value, or a date value can be normalized to a specific format.

How to use normalizes

The normalizes method can be used to normalize values of Active Record attributes. For example, the following code normalizes a string value to a numeric value before persisting it to the database

1
2
3
4
5
6
7
8
class User < ApplicationRecord
  normalizes :age, to: ->(value) { value.to_i }
end

user = User.create(age: nil)

# The value of age is now nil
user.age

Benefits of using normalizes

The normalizes method is a powerful new feature that can be used to improve the consistency and integrity of data in Rails applications. Here are some of the benefits of using normalizes:

Improved data consistency: normalizes can help to ensure that data is stored in the database in a consistent format. This can help to prevent errors and improve the performance of queries.

Improved data integrity: normalizes can help to ensure that data is valid and meets the requirements of the database. This can help to prevent data corruption and improve the security of applications.

Reduced development time: normalizes can help to reduce the amount of code that developers need to write to normalize data. This can free up developers to focus on other tasks.

Normalizes will also skip nil values by default

This means that we don’t have to use the &. safe operator when normalizing values. For example, the following code will normalize the age value to a numeric value, even if the age value is nil:

1
2
3
4
5
6
7
8
class User < ApplicationRecord
  normalizes :age, to: ->(value) { value.to_i }
end

user = User.create(age: nil)

# The value of age is now nil
user.age

Examples of how to use normalizes

Here are some examples of how the normalizes method can be used:

To normalize date and time values to specific formats of the database. For example, a database might require that date values be stored in the format YYYY-MM-DD. The normalizes method can be used to ensure that all date values are stored in this format.

To convert text values to formats specific of the database. For example, a database might require that text values be stored in the format UTF-8. The normalizes method can be used to ensure that all text values are stored in this format.

To validate values before persisting them to the database. For example, a database might require that all values be greater than or equal to 0. The normalizes method can be used to validate values before they are persisted to the database.

To apply transformations to values before persisting them to the database. For example, a database might require that all values be converted to uppercase. The normalizes method can be used to apply transformations to values before they are persisted to the database.