From 9ff8a9fa49a60ee3b50236f7d6094e43e0e90af3 Mon Sep 17 00:00:00 2001 From: Stephen Margheim Date: Wed, 2 Mar 2022 14:20:03 +0100 Subject: [PATCH 1/2] Use #attribute_names in ActiveRecord::Core#inspect This ensures that any attributes on an object in a serialized field that are no longer on the model schema are still reported via `inspect` --- activerecord/lib/active_record/core.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index 576f8db779..70b0867993 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -685,7 +685,7 @@ module ActiveRecord # We check defined?(@attributes) not to issue warnings if the object is # allocated but not initialized. inspection = if defined?(@attributes) && @attributes - self.class.attribute_names.filter_map do |name| + attribute_names.filter_map do |name| if _has_attribute?(name) "#{name}: #{attribute_for_inspect(name)}" end From 3c02ef95e95697fc69f6e8b4e7b1372396490123 Mon Sep 17 00:00:00 2001 From: Stephen Margheim Date: Thu, 10 Mar 2022 11:35:51 +0100 Subject: [PATCH 2/2] Add test for inspecting a Relation with a virtual field --- activerecord/test/cases/core_test.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/activerecord/test/cases/core_test.rb b/activerecord/test/cases/core_test.rb index 027fc1dd31..7f5b657053 100644 --- a/activerecord/test/cases/core_test.rb +++ b/activerecord/test/cases/core_test.rb @@ -50,6 +50,11 @@ class CoreTest < ActiveRecord::TestCase assert_equal "NonExistentTable(Table doesn't exist)", NonExistentTable.inspect end + def test_inspect_relation_with_virtual_field + relation = Topic.limit(1).select("1 as virtual_field") + assert_match(/virtual_field: 1/, relation.inspect) + end + def test_pretty_print_new topic = Topic.new actual = +""