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:
parent
b76b9e2164
commit
c433adffa2
2 changed files with 16 additions and 2 deletions
|
@ -515,7 +515,10 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def inspect
|
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
|
entries[10] = '...' if entries.size == 11
|
||||||
|
|
||||||
"#<#{self.class.name} [#{entries.join(', ')}]>"
|
"#<#{self.class.name} [#{entries.join(', ')}]>"
|
||||||
|
|
|
@ -1317,8 +1317,19 @@ class RelationTest < ActiveRecord::TestCase
|
||||||
assert_equal "#<ActiveRecord::Relation [#{Post.limit(2).map(&:inspect).join(', ')}]>", relation.inspect
|
assert_equal "#<ActiveRecord::Relation [#{Post.limit(2).map(&:inspect).join(', ')}]>", relation.inspect
|
||||||
end
|
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)
|
relation = Post.limit(11)
|
||||||
assert_equal "#<ActiveRecord::Relation [#{Post.limit(10).map(&:inspect).join(', ')}, ...]>", relation.inspect
|
assert_equal "#<ActiveRecord::Relation [#{Post.limit(10).map(&:inspect).join(', ')}, ...]>", relation.inspect
|
||||||
end
|
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue