From cce5126eb28764cc6dee2a9f92e2edcec4715fbe Mon Sep 17 00:00:00 2001 From: eileencodes Date: Wed, 18 Mar 2015 08:04:12 -0400 Subject: 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. --- activerecord/lib/active_record/reflection.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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 -- cgit v1.2.3