diff options
author | eileencodes <eileencodes@gmail.com> | 2015-03-18 08:04:12 -0400 |
---|---|---|
committer | eileencodes <eileencodes@gmail.com> | 2015-03-18 08:13:53 -0400 |
commit | cce5126eb28764cc6dee2a9f92e2edcec4715fbe (patch) | |
tree | f6881f61f9b77c0f649f05f4f6b36a0cb6ea4000 | |
parent | 8adfba360be5e2220208506a7b8d2089bd596199 (diff) | |
download | rails-cce5126eb28764cc6dee2a9f92e2edcec4715fbe.tar.gz rails-cce5126eb28764cc6dee2a9f92e2edcec4715fbe.tar.bz2 rails-cce5126eb28764cc6dee2a9f92e2edcec4715fbe.zip |
Add `clear_association_scope_cache` method
In the tests if I were to call `post.categorizations.to_a` and then later call
`post.categorizations.to_a` expecting to have different results the 2 queries
would be the same because of the caching involved in
`@association_scope_cache`. The chain gets cached and the queries will
be the same even if they are not supposed to be (i.e. testing an order
dependent scoping issue).
I found this issue because I was working on a bug with cached scoped
in hm:t and hm:t polymorphic relationships but `capture_sql` was
outputting the wrong SQL to write a good test.
-rw-r--r-- | activerecord/lib/active_record/reflection.rb | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index e4704b5b3e..4265afc0a5 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -370,6 +370,12 @@ module ActiveRecord [self] end + # This is for clearing cache on the reflection. Useful for tests that need to compare + # SQL queries on associations. + def clear_association_scope_cache # :nodoc: + @association_scope_cache.clear + end + def nested? false end @@ -706,6 +712,15 @@ module ActiveRecord end end + # This is for clearing cache on the reflection. Useful for tests that need to compare + # SQL queries on associations. + def clear_association_scope_cache # :nodoc: + @chain = nil + delegate_reflection.clear_association_scope_cache + source_reflection.clear_association_scope_cache + through_reflection.clear_association_scope_cache + end + # Consider the following example: # # class Person |