From 53a44b98b678ef29eea96650052e94114669dc73 Mon Sep 17 00:00:00 2001 From: Deepak Mahakale Date: Mon, 28 Aug 2023 13:16:22 +0530 Subject: [PATCH] [skip ci] Add ActiveRecord::Base.normalizes to 7.1 release notes Co-authored-by: Adrianna Chang --- guides/source/7_1_release_notes.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/guides/source/7_1_release_notes.md b/guides/source/7_1_release_notes.md index bce60d08172..14cffb8a07a 100644 --- a/guides/source/7_1_release_notes.md +++ b/guides/source/7_1_release_notes.md @@ -58,7 +58,31 @@ getting your Rails application up and running in a production environment. ### Add `ActiveRecord::Base.normalizes` -TODO: Add description https://github.com/rails/rails/pull/43945 +Normalizations can be declared for attribute values. The normalization +takes place when the attribute is assigned or updated, and will be persisted to the database. +Normalization is also applied to corresponding keyword arguments in finder methods, +allowing records to be queried using unnormalized values. + +For example: + +```ruby +class User < ActiveRecord::Base + normalizes :email, with: -> email { email.strip.downcase } + normalizes :phone, with: -> phone { phone.delete("^0-9").delete_prefix("1") } +end + +user = User.create(email: " CRUISE-CONTROL@EXAMPLE.COM\n") +user.email # => "cruise-control@example.com" + +user = User.find_by(email: "\tCRUISE-CONTROL@EXAMPLE.COM ") +user.email # => "cruise-control@example.com" +user.email_before_type_cast # => "cruise-control@example.com" + +User.exists?(email: "\tCRUISE-CONTROL@EXAMPLE.COM ") # => true +User.exists?(["email = ?", "\tCRUISE-CONTROL@EXAMPLE.COM "]) # => false + +User.normalize(:phone, "+1 (555) 867-5309") # => "5558675309" +``` ### Add `ActiveRecord::Base.generates_token_for`