From 843283b4dd9d4e08ac00296a2cd65ebccef3868f Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Sun, 10 Sep 2023 13:50:40 -0500 Subject: [PATCH] Add negative `where` examples for `normalizes` In #49105, `where` examples were added to the `normalizes` documentation to demonstrate that normalizations are also applied for `where`. However, as with the `exists?` examples, we should also demonstrate that normalizations are only applied to keyword arguments, not positional arguments. We can also address the original source of the confusion by changing the wording of "finder methods" to "query methods". This commit also removes the tests added in #49105. `normalizes` works at the level of attribute types, so there is no need to test every query method. Testing `find_by` is sufficient. (And, in point of fact, `find_by` is implemented in terms of `where`.) --- activerecord/CHANGELOG.md | 5 +++-- activerecord/lib/active_record/normalization.rb | 5 +++-- activerecord/test/cases/normalized_attribute_test.rb | 10 ---------- guides/source/7_1_release_notes.md | 5 +++-- 4 files changed, 9 insertions(+), 16 deletions(-) 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