diff options
author | Graham Turner <turnertgraham@gmail.com> | 2018-04-17 21:56:33 -0400 |
---|---|---|
committer | Graham Turner <turnertgraham@gmail.com> | 2018-04-26 12:41:15 -0400 |
commit | b07b97057fba21cbccca9a81630173a88406175a (patch) | |
tree | a1c38b265b84e65895ded21259e3ed9ec31fd7a1 /activerecord/lib/active_record/associations | |
parent | 2929d165c23f0d3976425a8e70de77847cc4b872 (diff) | |
download | rails-b07b97057fba21cbccca9a81630173a88406175a.tar.gz rails-b07b97057fba21cbccca9a81630173a88406175a.tar.bz2 rails-b07b97057fba21cbccca9a81630173a88406175a.zip |
Loaded associations should not run a new query when size is called
Already loaded associations were running an extra query when `size` was called on the association.
This fix ensures that an extra query is no longer run.
Update tests to use proper methods
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? |