Skip to content
Permalink
Browse files

Limit the number of records in Relation#inspect

While it's interesting to have the results array, it can make a console or a webpage freeze if there are a lot of them.
So this limits the number of records displayed in #inspect to 10 and tells how much were effectively found.
  • Loading branch information
dmathieu committed Jul 6, 2012
1 parent 717aa92 commit 7d0053e6a716be6345daa5e49dceda27ba8dfdb6
@@ -24,12 +24,14 @@
dealing with a `Relation` object rather than an array:.

User.where(:age => 30).inspect
# => <ActiveRecord::Relation [#<User ...>, #<User ...>]>
# => <ActiveRecord::Relation [#<User ...>, #<User ...>] ...>

User.where(:age => 30).to_a.inspect
# => [#<User ...>, #<User ...>]

*Brian Cardarella & Jon Leighton*
The number of records displayed will be limited to 10.

*Brian Cardarella, Jon Leighton & Damien Mathieu*

* Add `collation` and `ctype` support to PostgreSQL. These are available for PostgreSQL 8.4 or later.
Example:
@@ -515,7 +515,19 @@ def values
end

def inspect
"#<#{self.class.name} #{to_a.inspect}>"
text = if limit_value && limit_value <= 10
to_a.inspect
else
entries = limit(11).to_a
if entries.size > 10
entries.pop
"[#{entries.map(&:inspect).join(', ')}, ...]"
else
entries.inspect
end
end

"#<#{self.class.name} #{text}>"
end

private
@@ -1316,4 +1316,9 @@ def test_presence
relation = Post.limit(2)
assert_equal "#<ActiveRecord::Relation [#{Post.limit(2).map(&:inspect).join(', ')}]>", relation.inspect
end

test "relations limits the records in #inspect at 10" do
relation = Post.limit(11)
assert_equal "#<ActiveRecord::Relation [#{Post.limit(10).map(&:inspect).join(', ')}, ...]>", relation.inspect
end
end

0 comments on commit 7d0053e

Please sign in to comment.
You can’t perform that action at this time.