From 99049262d37fedcd25af91231423103b0d218694 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sat, 10 Apr 2021 21:25:44 +0900 Subject: [PATCH] Clear `@cache_keys` cache even when eager loading Follow up to #41789. --- activerecord/lib/active_record/relation.rb | 20 ++++--------------- .../active_record/relation/query_methods.rb | 2 +- .../test/cases/collection_cache_key_test.rb | 18 +++++++++++++++++ 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index f4900fab59..0ddedab269 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -467,11 +467,6 @@ module ActiveRecord def update_all(updates) raise ArgumentError, "Empty list of attributes to change" if updates.blank? - if eager_loading? - relation = apply_join_dependency - return relation.update_all(updates) - end - if updates.is_a?(Hash) if klass.locking_enabled? && !updates.key?(klass.locking_column) && @@ -484,11 +479,10 @@ module ActiveRecord values = Arel.sql(klass.sanitize_sql_for_assignment(updates, table.name)) end - source = arel.source.clone - source.left = table + arel = eager_loading? ? apply_join_dependency.arel : build_arel + arel.source.left = table stmt = arel.compile_update(values, table[primary_key]) - stmt.table(source) klass.connection.update(stmt, "#{klass} Update All").tap { reset } end @@ -607,16 +601,10 @@ module ActiveRecord raise ActiveRecordError.new("delete_all doesn't support #{invalid_methods.join(', ')}") end - if eager_loading? - relation = apply_join_dependency - return relation.delete_all - end - - source = arel.source.clone - source.left = table + arel = eager_loading? ? apply_join_dependency.arel : build_arel + arel.source.left = table stmt = arel.compile_delete(table[primary_key]) - stmt.from(source) klass.connection.delete(stmt, "#{klass} Destroy").tap { reset } end diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 8863d12b83..f6223f3c13 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -1225,7 +1225,7 @@ module ActiveRecord raise ImmutableRelation if defined?(@arel) && @arel end - def build_arel(aliases) + def build_arel(aliases = nil) arel = Arel::SelectManager.new(table) build_joins(arel.join_sources, aliases) diff --git a/activerecord/test/cases/collection_cache_key_test.rb b/activerecord/test/cases/collection_cache_key_test.rb index d62a2bd5a8..e51f19ccc3 100644 --- a/activerecord/test/cases/collection_cache_key_test.rb +++ b/activerecord/test/cases/collection_cache_key_test.rb @@ -111,6 +111,15 @@ module ActiveRecord assert_not_equal cache_key, developers.cache_key end + test "update_all with includes will update cache_key" do + developers = Developer.includes(:projects).where("projects.name": "Active Record") + cache_key = developers.cache_key + + developers.update_all(updated_at: Time.now.utc) + + assert_not_equal cache_key, developers.cache_key + end + test "delete_all will update cache_key" do developers = Developer.where(name: "David") cache_key = developers.cache_key @@ -120,6 +129,15 @@ module ActiveRecord assert_not_equal cache_key, developers.cache_key end + test "delete_all with includes will update cache_key" do + developers = Developer.includes(:projects).where("projects.name": "Active Record") + cache_key = developers.cache_key + + developers.delete_all + + assert_not_equal cache_key, developers.cache_key + end + test "destroy_all will update cache_key" do developers = Developer.where(name: "David") cache_key = developers.cache_key