diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2018-04-27 22:01:09 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-27 22:01:09 +0900 |
commit | 0a8fe15ba8fc7c36253996c557a0012f718d7d60 (patch) | |
tree | c67061f7bc8e6124649c7f8d6b2556f60d61b20b /activerecord/lib/active_record/associations | |
parent | 46b46e85c72f78e869a1b0038040849911c7e2c5 (diff) | |
parent | b07b97057fba21cbccca9a81630173a88406175a (diff) | |
download | rails-0a8fe15ba8fc7c36253996c557a0012f718d7d60.tar.gz rails-0a8fe15ba8fc7c36253996c557a0012f718d7d60.tar.bz2 rails-0a8fe15ba8fc7c36253996c557a0012f718d7d60.zip |
Merge pull request #32617 from tgturner/size-should-use-available-association
Loaded associations should not run a new query when size is called
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r-- | activerecord/lib/active_record/associations/collection_association.rb | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 671c4c56df..f233947fbd 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -212,6 +212,8 @@ module ActiveRecord def size if !find_target? || loaded? target.size + elsif @association_ids && target.empty? + @association_ids.size elsif !association_scope.group_values.empty? load_target.size elsif !association_scope.distinct_value && !target.empty? @@ -231,7 +233,7 @@ module ActiveRecord # loaded and you are going to fetch the records anyway it is better to # check <tt>collection.length.zero?</tt>. def empty? - if loaded? + if loaded? || @association_ids size.zero? else target.empty? && !scope.exists? |