mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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.
This commit is contained in:
parent
717aa92dd3
commit
7d0053e6a7
3 changed files with 22 additions and 3 deletions
|
@ -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 @@ module ActiveRecord
|
|||
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 @@ class RelationTest < ActiveRecord::TestCase
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue