From 2d12f800f1c6b0a2018346e0139301070273e46f Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Fri, 29 Mar 2019 18:21:56 +0900 Subject: Type cast falsy boolean symbols on boolean attribute as false Before 34cc301, type casting by boolean attribute when querying is a no-op, so finding by truthy boolean string (i.e. `where(value: "true") # => value = 'true'`) didn't work as expected (matches it to FALSE in MySQL #32624). By type casting is ensured, a value on boolean attribute is always serialized to TRUE or FALSE. In PostgreSQL, `where(value: :false) # => value = 'false'` was a valid SQL, so 34cc301 is a regresson for PostgreSQL since all symbol values are serialized as TRUE. I'd say using `:false` is mostly a developer's mistake (user's input basically comes as a string), but `:false` on boolean attribute is serialized as TRUE is not a desirable behavior for anybody. This allows falsy boolean symbols as false, i.e. `klass.create(value: :false).value? # => false` and `where(value: :false) # => value = FALSE`. Fixes #35676. --- activerecord/test/cases/boolean_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'activerecord') diff --git a/activerecord/test/cases/boolean_test.rb b/activerecord/test/cases/boolean_test.rb index ab9f974e2c..18824004d2 100644 --- a/activerecord/test/cases/boolean_test.rb +++ b/activerecord/test/cases/boolean_test.rb @@ -40,4 +40,13 @@ class BooleanTest < ActiveRecord::TestCase assert_equal b_false, Boolean.find_by(value: "false") assert_equal b_true, Boolean.find_by(value: "true") end + + def test_find_by_falsy_boolean_symbol + ActiveModel::Type::Boolean::FALSE_VALUES.each do |value| + b_false = Boolean.create!(value: value) + + assert_not_predicate b_false, :value? + assert_equal b_false, Boolean.find_by(id: b_false.id, value: value.to_s.to_sym) + end + end end -- cgit v1.2.3