1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Relation#inspect handles doesn't perform a new query on an already-loaded relation

This commit is contained in:
Jon Leighton 2012-07-07 10:07:28 +01:00
parent b76b9e2164
commit c433adffa2
2 changed files with 16 additions and 2 deletions

View file

@ -515,7 +515,10 @@ module ActiveRecord
end
def inspect
entries = limit([limit_value, 11].compact.min).map(&:inspect)
limit = [limit_value, 11].compact.min
entries = loaded? ? to_a.take(limit) : limit(limit)
entries.map!(&:inspect)
entries[10] = '...' if entries.size == 11
"#<#{self.class.name} [#{entries.join(', ')}]>"

View file

@ -1317,8 +1317,19 @@ class RelationTest < ActiveRecord::TestCase
assert_equal "#<ActiveRecord::Relation [#{Post.limit(2).map(&:inspect).join(', ')}]>", relation.inspect
end
test "relations limits the records in #inspect at 10" do
test "relations limit the records in #inspect at 10" do
relation = Post.limit(11)
assert_equal "#<ActiveRecord::Relation [#{Post.limit(10).map(&:inspect).join(', ')}, ...]>", relation.inspect
end
test "already-loaded relations don't perform a new query in #inspect" do
relation = Post.limit(2)
relation.to_a
expected = "#<ActiveRecord::Relation [#{Post.limit(2).map(&:inspect).join(', ')}]>"
assert_no_queries do
assert_equal expected, relation.inspect
end
end
end