diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2017-06-19 17:43:58 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-19 17:43:58 -0400 |
commit | a21687bfd6bf509acafbdbfe7d2f0d9ff57e323a (patch) | |
tree | ed13825030305cb191036c8c8c0c7ee46b221a28 | |
parent | 5cf76ccc8dc79d82595ff604b745fed78563b114 (diff) | |
parent | 95a91f392242d5cfcf3fb38b66c5645ceae009b8 (diff) | |
download | rails-a21687bfd6bf509acafbdbfe7d2f0d9ff57e323a.tar.gz rails-a21687bfd6bf509acafbdbfe7d2f0d9ff57e323a.tar.bz2 rails-a21687bfd6bf509acafbdbfe7d2f0d9ff57e323a.zip |
Merge pull request #29494 from kamipo/prevent_extra_scope_construction
Prevent extra `scope` construction in `find_target`
3 files changed, 5 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index ee2e79889f..5c45187d8f 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -276,7 +276,7 @@ module ActiveRecord end # Returns true if statement cache should be skipped on the association reader. - def skip_statement_cache? + def skip_statement_cache?(scope) reflection.has_scope? || scope.eager_loading? || klass.scope_attributes? || diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index edc53e2517..0cb17b47e8 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -300,7 +300,8 @@ module ActiveRecord private def find_target - return scope.to_a if skip_statement_cache? + scope = self.scope + return scope.to_a if skip_statement_cache?(scope) conn = klass.connection sc = reflection.association_scope_cache(conn, owner) do diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb index ca138635d2..2b426da607 100644 --- a/activerecord/lib/active_record/associations/singular_association.rb +++ b/activerecord/lib/active_record/associations/singular_association.rb @@ -36,7 +36,8 @@ module ActiveRecord end def find_target - return scope.take if skip_statement_cache? + scope = self.scope + return scope.take if skip_statement_cache?(scope) conn = klass.connection sc = reflection.association_scope_cache(conn, owner) do |