Skip to content

Commit 9904926

Browse files
committed
Clear @cache_keys cache even when eager loading
Follow up to rails#41789.
1 parent 64107f3 commit 9904926

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

activerecord/lib/active_record/relation.rb

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,6 @@ def _exec_scope(*args, &block) # :nodoc:
467467
def update_all(updates)
468468
raise ArgumentError, "Empty list of attributes to change" if updates.blank?
469469

470-
if eager_loading?
471-
relation = apply_join_dependency
472-
return relation.update_all(updates)
473-
end
474-
475470
if updates.is_a?(Hash)
476471
if klass.locking_enabled? &&
477472
!updates.key?(klass.locking_column) &&
@@ -484,11 +479,10 @@ def update_all(updates)
484479
values = Arel.sql(klass.sanitize_sql_for_assignment(updates, table.name))
485480
end
486481

487-
source = arel.source.clone
488-
source.left = table
482+
arel = eager_loading? ? apply_join_dependency.arel : build_arel
483+
arel.source.left = table
489484

490485
stmt = arel.compile_update(values, table[primary_key])
491-
stmt.table(source)
492486

493487
klass.connection.update(stmt, "#{klass} Update All").tap { reset }
494488
end
@@ -607,16 +601,10 @@ def delete_all
607601
raise ActiveRecordError.new("delete_all doesn't support #{invalid_methods.join(', ')}")
608602
end
609603

610-
if eager_loading?
611-
relation = apply_join_dependency
612-
return relation.delete_all
613-
end
614-
615-
source = arel.source.clone
616-
source.left = table
604+
arel = eager_loading? ? apply_join_dependency.arel : build_arel
605+
arel.source.left = table
617606

618607
stmt = arel.compile_delete(table[primary_key])
619-
stmt.from(source)
620608

621609
klass.connection.delete(stmt, "#{klass} Destroy").tap { reset }
622610
end

activerecord/lib/active_record/relation/query_methods.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,7 @@ def assert_mutability!
12251225
raise ImmutableRelation if defined?(@arel) && @arel
12261226
end
12271227

1228-
def build_arel(aliases)
1228+
def build_arel(aliases = nil)
12291229
arel = Arel::SelectManager.new(table)
12301230

12311231
build_joins(arel.join_sources, aliases)

activerecord/test/cases/collection_cache_key_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ class CollectionCacheKeyTest < ActiveRecord::TestCase
111111
assert_not_equal cache_key, developers.cache_key
112112
end
113113

114+
test "update_all with includes will update cache_key" do
115+
developers = Developer.includes(:projects).where("projects.name": "Active Record")
116+
cache_key = developers.cache_key
117+
118+
developers.update_all(updated_at: Time.now.utc)
119+
120+
assert_not_equal cache_key, developers.cache_key
121+
end
122+
114123
test "delete_all will update cache_key" do
115124
developers = Developer.where(name: "David")
116125
cache_key = developers.cache_key
@@ -120,6 +129,15 @@ class CollectionCacheKeyTest < ActiveRecord::TestCase
120129
assert_not_equal cache_key, developers.cache_key
121130
end
122131

132+
test "delete_all with includes will update cache_key" do
133+
developers = Developer.includes(:projects).where("projects.name": "Active Record")
134+
cache_key = developers.cache_key
135+
136+
developers.delete_all
137+
138+
assert_not_equal cache_key, developers.cache_key
139+
end
140+
123141
test "destroy_all will update cache_key" do
124142
developers = Developer.where(name: "David")
125143
cache_key = developers.cache_key

0 commit comments

Comments
 (0)