From afeb756828853d7df59f776df9ae1f09227bfca1 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Fri, 7 Aug 2020 04:33:21 +0900 Subject: [PATCH] Fix deserializing enum mapping nil Follow up to #38086. User assigned nil is type casted by #38086, but loaded nil from database does not yet, this fixes that. --- activerecord/lib/active_record/enum.rb | 1 - activerecord/test/cases/enum_test.rb | 4 ++++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index f7aebc5ecb..c243bcc44d 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -136,7 +136,6 @@ module ActiveRecord end def deserialize(value) - return if value.nil? mapping.key(subtype.deserialize(value)) end diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb index 27ec1a5a62..394ca511a4 100644 --- a/activerecord/test/cases/enum_test.rb +++ b/activerecord/test/cases/enum_test.rb @@ -236,6 +236,10 @@ class EnumTest < ActiveRecord::TestCase assert_nil @book.reload.status end + test "deserialize nil value to enum which defines nil value to hash" do + assert_equal "forgotten", books(:ddd).last_read + end + test "assign nil value" do @book.status = nil assert_nil @book.status