diff options
author | Jon Leighton <j@jonathanleighton.com> | 2012-07-07 10:07:28 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2012-07-07 10:07:28 +0100 |
commit | c433adffa23785d3727379902859f57bc3f68b27 (patch) | |
tree | c83cff08a7ede470cfaad49cf9f8b9ee6e2c1319 /activerecord | |
parent | b76b9e216463f045c8a5ca4b1a6a7994f2baa389 (diff) | |
download | rails-c433adffa23785d3727379902859f57bc3f68b27.tar.gz rails-c433adffa23785d3727379902859f57bc3f68b27.tar.bz2 rails-c433adffa23785d3727379902859f57bc3f68b27.zip |
Relation#inspect handles doesn't perform a new query on an already-loaded relation
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 5 | ||||
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 13 |
2 files changed, 16 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 0d1c9ef3e5..a39328b89b 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -515,7 +515,10 @@ module ActiveRecord end 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 "#<#{self.class.name} [#{entries.join(', ')}]>" diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index bdf14e0a85..8713b8d5e4 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -1317,8 +1317,19 @@ class RelationTest < ActiveRecord::TestCase assert_equal "#<ActiveRecord::Relation [#{Post.limit(2).map(&:inspect).join(', ')}]>", relation.inspect 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) assert_equal "#<ActiveRecord::Relation [#{Post.limit(10).map(&:inspect).join(', ')}, ...]>", relation.inspect 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 |