mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
parent
64107f367a
commit
99049262d3
3 changed files with 23 additions and 17 deletions
|
@ -467,11 +467,6 @@ module ActiveRecord
|
||||||
def update_all(updates)
|
def update_all(updates)
|
||||||
raise ArgumentError, "Empty list of attributes to change" if updates.blank?
|
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 updates.is_a?(Hash)
|
||||||
if klass.locking_enabled? &&
|
if klass.locking_enabled? &&
|
||||||
!updates.key?(klass.locking_column) &&
|
!updates.key?(klass.locking_column) &&
|
||||||
|
@ -484,11 +479,10 @@ module ActiveRecord
|
||||||
values = Arel.sql(klass.sanitize_sql_for_assignment(updates, table.name))
|
values = Arel.sql(klass.sanitize_sql_for_assignment(updates, table.name))
|
||||||
end
|
end
|
||||||
|
|
||||||
source = arel.source.clone
|
arel = eager_loading? ? apply_join_dependency.arel : build_arel
|
||||||
source.left = table
|
arel.source.left = table
|
||||||
|
|
||||||
stmt = arel.compile_update(values, table[primary_key])
|
stmt = arel.compile_update(values, table[primary_key])
|
||||||
stmt.table(source)
|
|
||||||
|
|
||||||
klass.connection.update(stmt, "#{klass} Update All").tap { reset }
|
klass.connection.update(stmt, "#{klass} Update All").tap { reset }
|
||||||
end
|
end
|
||||||
|
@ -607,16 +601,10 @@ module ActiveRecord
|
||||||
raise ActiveRecordError.new("delete_all doesn't support #{invalid_methods.join(', ')}")
|
raise ActiveRecordError.new("delete_all doesn't support #{invalid_methods.join(', ')}")
|
||||||
end
|
end
|
||||||
|
|
||||||
if eager_loading?
|
arel = eager_loading? ? apply_join_dependency.arel : build_arel
|
||||||
relation = apply_join_dependency
|
arel.source.left = table
|
||||||
return relation.delete_all
|
|
||||||
end
|
|
||||||
|
|
||||||
source = arel.source.clone
|
|
||||||
source.left = table
|
|
||||||
|
|
||||||
stmt = arel.compile_delete(table[primary_key])
|
stmt = arel.compile_delete(table[primary_key])
|
||||||
stmt.from(source)
|
|
||||||
|
|
||||||
klass.connection.delete(stmt, "#{klass} Destroy").tap { reset }
|
klass.connection.delete(stmt, "#{klass} Destroy").tap { reset }
|
||||||
end
|
end
|
||||||
|
|
|
@ -1225,7 +1225,7 @@ module ActiveRecord
|
||||||
raise ImmutableRelation if defined?(@arel) && @arel
|
raise ImmutableRelation if defined?(@arel) && @arel
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_arel(aliases)
|
def build_arel(aliases = nil)
|
||||||
arel = Arel::SelectManager.new(table)
|
arel = Arel::SelectManager.new(table)
|
||||||
|
|
||||||
build_joins(arel.join_sources, aliases)
|
build_joins(arel.join_sources, aliases)
|
||||||
|
|
|
@ -111,6 +111,15 @@ module ActiveRecord
|
||||||
assert_not_equal cache_key, developers.cache_key
|
assert_not_equal cache_key, developers.cache_key
|
||||||
end
|
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
|
test "delete_all will update cache_key" do
|
||||||
developers = Developer.where(name: "David")
|
developers = Developer.where(name: "David")
|
||||||
cache_key = developers.cache_key
|
cache_key = developers.cache_key
|
||||||
|
@ -120,6 +129,15 @@ module ActiveRecord
|
||||||
assert_not_equal cache_key, developers.cache_key
|
assert_not_equal cache_key, developers.cache_key
|
||||||
end
|
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
|
test "destroy_all will update cache_key" do
|
||||||
developers = Developer.where(name: "David")
|
developers = Developer.where(name: "David")
|
||||||
cache_key = developers.cache_key
|
cache_key = developers.cache_key
|
||||||
|
|
Loading…
Reference in a new issue