diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-08-12 21:14:42 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-08-12 21:17:30 -0300 |
commit | dc3230b156a4cfe5a8fbe3636edf0117f8e122cc (patch) | |
tree | 39d6984b72453429a1bd1752bc604fa82dce006d /activerecord/lib/active_record | |
parent | a4f780947299cc35f14c9e1825278155ae88ee44 (diff) | |
download | rails-dc3230b156a4cfe5a8fbe3636edf0117f8e122cc.tar.gz rails-dc3230b156a4cfe5a8fbe3636edf0117f8e122cc.tar.bz2 rails-dc3230b156a4cfe5a8fbe3636edf0117f8e122cc.zip |
Skip statement cache on through association reader
If the through class has default scopes we should skip the statement
cache.
Closes #20745.
Diffstat (limited to 'activerecord/lib/active_record')
3 files changed, 10 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index 7c729676a7..c7b396f3d4 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -251,6 +251,14 @@ module ActiveRecord initialize_attributes(record) end end + + # Returns true if statement cache should be skipped on the association reader. + def skip_statement_cache? + reflection.scope_chain.any?(&:any?) || + scope.eager_loading? || + klass.scope_attributes? || + reflection.source_reflection.active_record.default_scopes.any? + end end end end diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 0fc2b83b71..256df3ca11 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -442,12 +442,7 @@ module ActiveRecord private def get_records - if reflection.scope_chain.any?(&:any?) || - scope.eager_loading? || - klass.scope_attributes? - - return scope.to_a - end + return scope.to_a if skip_statement_cache? 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 30c5d72482..03cb8cb8c3 100644 --- a/activerecord/lib/active_record/associations/singular_association.rb +++ b/activerecord/lib/active_record/associations/singular_association.rb @@ -47,12 +47,7 @@ module ActiveRecord end def get_records - if reflection.scope_chain.any?(&:any?) || - scope.eager_loading? || - klass.scope_attributes? - - return scope.limit(1).to_a - end + return scope.limit(1).to_a if skip_statement_cache? conn = klass.connection sc = reflection.association_scope_cache(conn, owner) do |