diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index 8ff743ee66..751beb59cd 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -144,7 +144,7 @@ module ActiveRecord end def serialize(value) - mapping.fetch(value, value) + subtype.serialize(mapping.fetch(value, value)) end def assert_valid_value(value) diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb index 6e899efb6c..261f957a7a 100644 --- a/activerecord/test/cases/enum_test.rb +++ b/activerecord/test/cases/enum_test.rb @@ -81,6 +81,14 @@ class EnumTest < ActiveRecord::TestCase assert_equal books(:ddd), Book.where(last_read: "forgotten").first end + test "find via where should be type casted" do + book = Book.enabled.create! + assert_predicate book, :enabled? + + enabled = Book.boolean_statuses[:enabled].to_s + assert_equal book, Book.where(boolean_status: enabled).last + end + test "build from scope" do assert_predicate Book.written.build, :written? assert_not_predicate Book.written.build, :proposed?