diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-06-18 19:59:09 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2017-06-18 19:59:09 +0900 |
commit | 95a91f392242d5cfcf3fb38b66c5645ceae009b8 (patch) | |
tree | 81627be17e3c3fc39e8ae904a2537700b063add7 /activerecord | |
parent | 67a579419b275c4c982b02d9b6380cf4cbdaba20 (diff) | |
download | rails-95a91f392242d5cfcf3fb38b66c5645ceae009b8.tar.gz rails-95a91f392242d5cfcf3fb38b66c5645ceae009b8.tar.bz2 rails-95a91f392242d5cfcf3fb38b66c5645ceae009b8.zip |
Prevent extra `scope` construction in `find_target`
Because constructing `scope` is a little expensive.
Diffstat (limited to 'activerecord')
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 |