Skip to content

Commit 9fd3d03

Browse files
authored
Merge pull request rails#49105 from Earlopain/ar-normalizes-where
Document `where` support for AR `normalizes`
2 parents d3981d1 + 830957e commit 9fd3d03

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

activerecord/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@
862862
```ruby
863863
class User < ActiveRecord::Base
864864
normalizes :email, with: -> email { email.strip.downcase }
865+
normalizes :phone, with: -> phone { phone.delete("^0-9").delete_prefix("1") }
865866
end
866867
867868
user = User.create(email: " [email protected]\n")
@@ -871,8 +872,12 @@
871872
user.email # => "[email protected]"
872873
user.email_before_type_cast # => "[email protected]"
873874
875+
User.where(email: "\t[email protected] ").count # => 1
876+
874877
User.exists?(email: "\t[email protected] ") # => true
875878
User.exists?(["email = ?", "\t[email protected] "]) # => false
879+
880+
User.normalize_value_for(:phone, "+1 (555) 867-5309") # => "5558675309"
876881
```
877882

878883
*Jonathan Hefner*

activerecord/lib/active_record/normalization.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ module ClassMethods
6969
# user.email # => "[email protected]"
7070
# user.email_before_type_cast # => "[email protected]"
7171
#
72+
# User.where(email: "\[email protected] ").count # => 1
73+
#
7274
# User.exists?(email: "\[email protected] ") # => true
7375
# User.exists?(["email = ?", "\[email protected] "]) # => false
7476
#

activerecord/test/cases/normalized_attribute_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ class NormalizedAircraft < Aircraft
8383
assert_equal @aircraft, NormalizedAircraft.find_by(manufactured_at: @time.to_s)
8484
end
8585

86+
test "searches a record by normalized value" do
87+
from_database = NormalizedAircraft.where(name: "fly HIGH")
88+
assert_equal [@aircraft], from_database
89+
end
90+
91+
test "searches a record by normalized values" do
92+
from_database = NormalizedAircraft.where(name: ["fly LOW", "fly HIGH"])
93+
assert_equal [@aircraft], from_database
94+
end
95+
8696
test "can stack normalizations" do
8797
titlecase_then_reverse = Class.new(NormalizedAircraft) do
8898
normalizes :name, with: -> name { name.reverse }

guides/source/7_1_release_notes.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ user = User.find_by(email: "\[email protected] ")
7878
user.email # => "[email protected]"
7979
user.email_before_type_cast # => "[email protected]"
8080

81+
User.where(email: "\t[email protected] ").count # => 1
82+
8183
User.exists?(email: "\t[email protected] ") # => true
8284
User.exists?(["email = ?", "\t[email protected] "]) # => false
8385

84-
User.normalize(:phone, "+1 (555) 867-5309") # => "5558675309"
86+
User.normalize_value_for(:phone, "+1 (555) 867-5309") # => "5558675309"
8587
```
8688

8789
### Add `ActiveRecord::Base.generates_token_for`

0 commit comments

Comments
 (0)