提交 25fb6cc2 编写于 作者: J José Valim

Merge pull request #6987 from dmathieu/limit_inspect

Limit the number of records in Relation#inspect
......@@ -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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册