1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Clear @cache_keys cache even when eager loading

Follow up to #41789.
This commit is contained in:
Ryuta Kamizono 2021-04-10 21:25:44 +09:00
parent 64107f367a
commit 99049262d3
3 changed files with 23 additions and 17 deletions

View file

@ -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

View file

@ -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)

View file

@ -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