diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2017-03-29 12:59:47 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-29 12:59:47 -0400 |
commit | eccc149be362ccf12446ed1c8287e011731a817b (patch) | |
tree | 5b25e44a6239f118c8df5aab99021c43659e6115 /activerecord | |
parent | 098e94caf6515c92334b03af4c8358e39a4201a5 (diff) | |
parent | 3d6176fded424a2195e399d031e343bbcd0edcad (diff) | |
download | rails-eccc149be362ccf12446ed1c8287e011731a817b.tar.gz rails-eccc149be362ccf12446ed1c8287e011731a817b.tar.bz2 rails-eccc149be362ccf12446ed1c8287e011731a817b.zip |
Merge pull request #28592 from htanata/ar_relation_inspect_should_not_load_all_records
Load only needed records on ActiveRecord::Relation#inspect
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 9 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 6 |
3 files changed, 18 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 30d580b9e3..3ea9976286 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1 +1,10 @@ +* Load only needed records on `ActiveRecord::Relation#inspect`. + + Instead of loading all records and returning only a subset of those, just + load the records as needed. + + Fixes #25537. + + *Hendy Tanata* + Please check [5-1-stable](https://github.com/rails/rails/blob/5-1-stable/activerecord/CHANGELOG.md) for previous changes. diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 2d6b21bec5..5775eda5a5 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -635,7 +635,9 @@ module ActiveRecord end def inspect - entries = records.take([limit_value, 11].compact.min).map!(&:inspect) + subject = loaded? ? records : self + entries = subject.take([limit_value, 11].compact.min).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 8c06b1537a..856469c710 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -1901,6 +1901,12 @@ class RelationTest < ActiveRecord::TestCase assert_equal "#<ActiveRecord::Relation [#{Post.limit(10).map(&:inspect).join(', ')}, ...]>", relation.inspect end + test "relations don't load all records in #inspect" do + assert_sql(/LIMIT/) do + Post.all.inspect + end + end + test "already-loaded relations don't perform a new query in #inspect" do relation = Post.limit(2) relation.to_a |