diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 9e3ec475dda50..188e39eba693f 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -944,7 +944,7 @@ A normalization is applied when the attribute is assigned or updated, and the normalized value will be persisted to the database. The normalization - is also applied to the corresponding keyword argument of finder methods. + is also applied to the corresponding keyword argument of query methods. This allows a record to be created and later queried using unnormalized values. For example: @@ -961,7 +961,8 @@ user.email # => "cruise-control@example.com" user.email_before_type_cast # => "cruise-control@example.com" - User.where(email: "\tCRUISE-CONTROL@EXAMPLE.COM ").count # => 1 + User.where(email: "\tCRUISE-CONTROL@EXAMPLE.COM ").count # => 1 + User.where(["email = ?", "\tCRUISE-CONTROL@EXAMPLE.COM "]).count # => 0 User.exists?(email: "\tCRUISE-CONTROL@EXAMPLE.COM ") # => true User.exists?(["email = ?", "\tCRUISE-CONTROL@EXAMPLE.COM "]) # => false diff --git a/activerecord/lib/active_record/normalization.rb b/activerecord/lib/active_record/normalization.rb index 2a6667006031c..3f4872bef40f7 100644 --- a/activerecord/lib/active_record/normalization.rb +++ b/activerecord/lib/active_record/normalization.rb @@ -32,7 +32,7 @@ module ClassMethods # Declares a normalization for one or more attributes. The normalization # is applied when the attribute is assigned or updated, and the normalized # value will be persisted to the database. The normalization is also - # applied to the corresponding keyword argument of finder methods. This + # applied to the corresponding keyword argument of query methods. This # allows a record to be created and later queried using unnormalized # values. # @@ -69,7 +69,8 @@ module ClassMethods # user.email # => "cruise-control@example.com" # user.email_before_type_cast # => "cruise-control@example.com" # - # User.where(email: "\tCRUISE-CONTROL@EXAMPLE.COM ").count # => 1 + # User.where(email: "\tCRUISE-CONTROL@EXAMPLE.COM ").count # => 1 + # User.where(["email = ?", "\tCRUISE-CONTROL@EXAMPLE.COM "]).count # => 0 # # User.exists?(email: "\tCRUISE-CONTROL@EXAMPLE.COM ") # => true # User.exists?(["email = ?", "\tCRUISE-CONTROL@EXAMPLE.COM "]) # => false diff --git a/activerecord/test/cases/normalized_attribute_test.rb b/activerecord/test/cases/normalized_attribute_test.rb index 506f8b7b2d6cc..8d2bdf26aa060 100644 --- a/activerecord/test/cases/normalized_attribute_test.rb +++ b/activerecord/test/cases/normalized_attribute_test.rb @@ -83,16 +83,6 @@ class NormalizedAircraft < Aircraft assert_equal @aircraft, NormalizedAircraft.find_by(manufactured_at: @time.to_s) end - test "searches a record by normalized value" do - from_database = NormalizedAircraft.where(name: "fly HIGH") - assert_equal [@aircraft], from_database - end - - test "searches a record by normalized values" do - from_database = NormalizedAircraft.where(name: ["fly LOW", "fly HIGH"]) - assert_equal [@aircraft], from_database - end - test "can stack normalizations" do titlecase_then_reverse = Class.new(NormalizedAircraft) do normalizes :name, with: -> name { name.reverse } diff --git a/guides/source/7_1_release_notes.md b/guides/source/7_1_release_notes.md index 5228609900fff..0539c60cea811 100644 --- a/guides/source/7_1_release_notes.md +++ b/guides/source/7_1_release_notes.md @@ -60,7 +60,7 @@ getting your Rails application up and running in a production environment. 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, +Normalization is also applied to corresponding keyword arguments in query methods, allowing records to be queried using unnormalized values. For example: @@ -78,7 +78,8 @@ 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.where(email: "\tCRUISE-CONTROL@EXAMPLE.COM ").count # => 1 +User.where(email: "\tCRUISE-CONTROL@EXAMPLE.COM ").count # => 1 +User.where(["email = ?", "\tCRUISE-CONTROL@EXAMPLE.COM "]).count # => 0 User.exists?(email: "\tCRUISE-CONTROL@EXAMPLE.COM ") # => true User.exists?(["email = ?", "\tCRUISE-CONTROL@EXAMPLE.COM "]) # => false