Normalizing Attributes using ActiveRecord::Base::normalizes

Dam Tien HungDam Tien Hung
2 min read

ActiveRecord::Base::normalizes

Rails 7.1 add ActiveRecord::Base::normalizes API. ActiveRecord::Base::normalizes is a method provided by the normalize gem that is used to normalize data before it is saved to the database. Essentially, normalization is the process of transforming data to a standardized format that can be easily used and queried by your application.

Options

  • :with - The normalization to apply.

  • :apply_to_nil - Whether to apply the normalization to nil values. Defaults to false.

Options - :with

Before Rails 7.1, need to do the following.

class User < ApplicationRecord
  before_save :strip_downcase_email, if: :email_present?
  before_save :format_phone, if: :phone_present?

  private

  def email_present?
    email.present?
  end

  def strip_downcase_email
    email.strip.downcase!
  end

  def phone_present?
    phone.present?
  end

  def format_phone
    phone.delete!("^0-9").delete_prefix!("1")
  end
end

In Rails 7.1, you can refactor the above to the below code.

class User < ApplicationRecord
  normalizes :email, with: -> email { email.strip.downcase }
  normalizes :phone, with: -> phone { phone.delete("^0-9").delete_prefix("1") }
end

In this example, we're defining a custom normalizer that will strip leading and trailing whitespace from the email and phone attributes, and convert the email to lowercase, and phone format to the correct format. This custom normalizer will be used instead of the default normalizer when saving User objects.

Options - :apply_to_nil

The apply_to_nil option allows you to specify whether the normalization rule should be applied to nil (unset) attributes. By default, this option is set to false, which means that the normalization rule will only be applied to attributes that have a value. However, you can set apply_to_nil to true if you want the normalization rule to be applied to nil attributes as well.

Here's an example of using the apply_to_nil option:

# model

class Book < ActiveRecord::Base
  normalizes :title, with: :strip, apply_to_nil: true
end

# console

book = Book.new(title: nil)
book.save
puts book.title # outputs an empty string ('')

In this case, the title attribute is set to an empty string ('') after the normalization rule is applied, because apply_to_nil is set to true. If apply_to_nil was set to false, the title attribute would remain nil.


Note

Keep in mind that normalization can be a complex process, and you should carefully consider the requirements of your application before using ActiveRecord::Base::normalizes. It's also important to test your code thoroughly to ensure that normalization does not introduce any unexpected issues or data loss.

Please check out this pull request for more details.

2
Subscribe to my newsletter

Read articles from Dam Tien Hung directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dam Tien Hung
Dam Tien Hung