Skip to content
This repository
Browse code

Merge pull request #6987 from dmathieu/limit_inspect

Limit the number of records in Relation#inspect
  • Loading branch information...
commit 25fb6cc2f3243f476c34e584e8de89a9fe8b03e2 2 parents 9f5d2b1 + 7d0053e
José Valim josevalim authored
6 activerecord/CHANGELOG.md
Source Rendered
@@ -24,12 +24,14 @@
24 24 dealing with a `Relation` object rather than an array:.
25 25
26 26 User.where(:age => 30).inspect
27   - # => <ActiveRecord::Relation [#<User ...>, #<User ...>]>
  27 + # => <ActiveRecord::Relation [#<User ...>, #<User ...>] ...>
28 28
29 29 User.where(:age => 30).to_a.inspect
30 30 # => [#<User ...>, #<User ...>]
31 31
32   - *Brian Cardarella & Jon Leighton*
  32 + The number of records displayed will be limited to 10.
  33 +
  34 + *Brian Cardarella, Jon Leighton & Damien Mathieu*
33 35
34 36 * Add `collation` and `ctype` support to PostgreSQL. These are available for PostgreSQL 8.4 or later.
35 37 Example:
14 activerecord/lib/active_record/relation.rb
@@ -515,7 +515,19 @@ def values
515 515 end
516 516
517 517 def inspect
518   - "#<#{self.class.name} #{to_a.inspect}>"
  518 + text = if limit_value && limit_value <= 10
  519 + to_a.inspect
  520 + else
  521 + entries = limit(11).to_a
  522 + if entries.size > 10
  523 + entries.pop
  524 + "[#{entries.map(&:inspect).join(', ')}, ...]"
  525 + else
  526 + entries.inspect
  527 + end
  528 + end
  529 +
  530 + "#<#{self.class.name} #{text}>"
519 531 end
520 532
521 533 private
5 activerecord/test/cases/relations_test.rb
@@ -1316,4 +1316,9 @@ def test_presence
1316 1316 relation = Post.limit(2)
1317 1317 assert_equal "#<ActiveRecord::Relation [#{Post.limit(2).map(&:inspect).join(', ')}]>", relation.inspect
1318 1318 end
  1319 +
  1320 + test "relations limits the records in #inspect at 10" do
  1321 + relation = Post.limit(11)
  1322 + assert_equal "#<ActiveRecord::Relation [#{Post.limit(10).map(&:inspect).join(', ')}, ...]>", relation.inspect
  1323 + end
1319 1324 end

0 comments on commit 25fb6cc

Please sign in to comment.
Something went wrong with that request. Please try again.