aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/core_test.rb
diff options
context:
space:
mode:
authorEugene Kenny <elkenny@gmail.com>2018-11-06 01:51:52 +0000
committerEugene Kenny <elkenny@gmail.com>2018-11-06 01:51:52 +0000
commit65cd0fda2572ac9c78d8582496a9009d0c48df08 (patch)
treea1775a4f7690423034d60e4168501efc45d268f4 /activerecord/test/cases/core_test.rb
parenta8c06c2d7dfa49a9fede1eec65c1058fe0fac82b (diff)
downloadrails-65cd0fda2572ac9c78d8582496a9009d0c48df08.tar.gz
rails-65cd0fda2572ac9c78d8582496a9009d0c48df08.tar.bz2
rails-65cd0fda2572ac9c78d8582496a9009d0c48df08.zip
Fix inspect with non-primary key id attribute
The `read_attribute` method always returns the primary key when asked to read the `id` attribute, even if the primary key isn't named `id`, and even if another attribute named `id` exists. For the `inspect`, `attribute_for_inspect` and `pretty_print` methods, this behaviour is undesirable, as they're used to examine the internal state of the record. By using `_read_attribute` instead, we'll get the real value of the `id` attribute.
Diffstat (limited to 'activerecord/test/cases/core_test.rb')
-rw-r--r--activerecord/test/cases/core_test.rb12
1 files changed, 12 insertions, 0 deletions
diff --git a/activerecord/test/cases/core_test.rb b/activerecord/test/cases/core_test.rb
index f7fbf3ee8a..36e3d543cd 100644
--- a/activerecord/test/cases/core_test.rb
+++ b/activerecord/test/cases/core_test.rb
@@ -30,6 +30,11 @@ class CoreTest < ActiveRecord::TestCase
assert_equal %(#<Topic id: 1, title: "The First Topic">), Topic.all.merge!(select: "id, title", where: "id = 1").first.inspect
end
+ def test_inspect_instance_with_non_primary_key_id_attribute
+ topic = topics(:first).becomes(TitlePrimaryKeyTopic)
+ assert_match(/id: 1/, topic.inspect)
+ end
+
def test_inspect_class_without_table
assert_equal "NonExistentTable(Table doesn't exist)", NonExistentTable.inspect
end
@@ -110,4 +115,11 @@ class CoreTest < ActiveRecord::TestCase
PP.pp(subtopic.new, StringIO.new(actual))
assert_equal "inspecting topic\n", actual
end
+
+ def test_pretty_print_with_non_primary_key_id_attribute
+ topic = topics(:first).becomes(TitlePrimaryKeyTopic)
+ actual = +""
+ PP.pp(topic, StringIO.new(actual))
+ assert_match(/id: 1/, actual)
+ end
end