Skip to content

Commit 53a44b9

Browse files
[skip ci] Add ActiveRecord::Base.normalizes to 7.1 release notes
Co-authored-by: Adrianna Chang <[email protected]>
1 parent 80592d6 commit 53a44b9

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

guides/source/7_1_release_notes.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,31 @@ getting your Rails application up and running in a production environment.
5858

5959
### Add `ActiveRecord::Base.normalizes`
6060

61-
TODO: Add description https://github.com/rails/rails/pull/43945
61+
Normalizations can be declared for attribute values. The normalization
62+
takes place when the attribute is assigned or updated, and will be persisted to the database.
63+
Normalization is also applied to corresponding keyword arguments in finder methods,
64+
allowing records to be queried using unnormalized values.
65+
66+
For example:
67+
68+
```ruby
69+
class User < ActiveRecord::Base
70+
normalizes :email, with: -> email { email.strip.downcase }
71+
normalizes :phone, with: -> phone { phone.delete("^0-9").delete_prefix("1") }
72+
end
73+
74+
user = User.create(email: " [email protected]\n")
75+
user.email # => "[email protected]"
76+
77+
user = User.find_by(email: "\t[email protected] ")
78+
user.email # => "[email protected]"
79+
user.email_before_type_cast # => "[email protected]"
80+
81+
User.exists?(email: "\t[email protected] ") # => true
82+
User.exists?(["email = ?", "\t[email protected] "]) # => false
83+
84+
User.normalize(:phone, "+1 (555) 867-5309") # => "5558675309"
85+
```
6286

6387
### Add `ActiveRecord::Base.generates_token_for`
6488

0 commit comments

Comments
 (0)