diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2014-10-14 16:16:13 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2014-10-14 16:16:29 -0700 |
commit | b59d47d82e6ecb4308b675875b5a62c79858645e (patch) | |
tree | 85ecbf4d959d64f4ba7a69f87af52943231615e7 /activerecord | |
parent | 6bb040b55748269dd2cb2ae3a36fa54ca7c70c5e (diff) | |
download | rails-b59d47d82e6ecb4308b675875b5a62c79858645e.tar.gz rails-b59d47d82e6ecb4308b675875b5a62c79858645e.tar.bz2 rails-b59d47d82e6ecb4308b675875b5a62c79858645e.zip |
break cache if we're inside a "scoping" call. fixes #17052
For now, we don't want to take "scoping" calls in to account when
calculating cache keys for relations, so just opt-out.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/associations/singular_association.rb | 7 | ||||
-rw-r--r-- | activerecord/test/cases/scoping/relation_scoping_test.rb | 10 |
2 files changed, 16 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/singular_association.rb b/activerecord/lib/active_record/associations/singular_association.rb index b9326b9683..c360ef1b2c 100644 --- a/activerecord/lib/active_record/associations/singular_association.rb +++ b/activerecord/lib/active_record/associations/singular_association.rb @@ -39,7 +39,12 @@ module ActiveRecord end def get_records - return scope.limit(1).to_a if reflection.scope_chain.any?(&:any?) || scope.eager_loading? + if reflection.scope_chain.any?(&:any?) || + scope.eager_loading? || + klass.current_scope + + return scope.limit(1).to_a + end conn = klass.connection sc = reflection.association_scope_cache(conn, owner) do diff --git a/activerecord/test/cases/scoping/relation_scoping_test.rb b/activerecord/test/cases/scoping/relation_scoping_test.rb index 8e512e118a..c63fea15f2 100644 --- a/activerecord/test/cases/scoping/relation_scoping_test.rb +++ b/activerecord/test/cases/scoping/relation_scoping_test.rb @@ -15,6 +15,16 @@ class RelationScopingTest < ActiveRecord::TestCase developers(:david) end + def test_unscoped_breaks_caching + author = authors :mary + assert_nil author.first_post + post = FirstPost.unscoped do + author = authors :mary + author.reload.first_post + end + assert post + end + def test_reverse_order assert_equal Developer.order("id DESC").to_a.reverse, Developer.order("id DESC").reverse_order end |