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

fixing update_all and delete_all when chained with left_joins. fixes #27192

This commit is contained in:
Diego Plentz 2016-11-27 19:47:51 -02:00
parent c638d47872
commit 8bd7735a2c
2 changed files with 28 additions and 2 deletions

View file

@ -373,7 +373,7 @@ module ActiveRecord
stmt.set Arel.sql(@klass.send(:sanitize_sql_for_assignment, updates)) stmt.set Arel.sql(@klass.send(:sanitize_sql_for_assignment, updates))
stmt.table(table) stmt.table(table)
if joins_values.any? if has_join_values?
@klass.connection.join_to_update(stmt, arel, arel_attribute(primary_key)) @klass.connection.join_to_update(stmt, arel, arel_attribute(primary_key))
else else
stmt.key = arel_attribute(primary_key) stmt.key = arel_attribute(primary_key)
@ -522,7 +522,7 @@ module ActiveRecord
stmt = Arel::DeleteManager.new stmt = Arel::DeleteManager.new
stmt.from(table) stmt.from(table)
if joins_values.any? if has_join_values?
@klass.connection.join_to_delete(stmt, arel, arel_attribute(primary_key)) @klass.connection.join_to_delete(stmt, arel, arel_attribute(primary_key))
else else
stmt.wheres = arel.constraints stmt.wheres = arel.constraints
@ -680,6 +680,10 @@ module ActiveRecord
private private
def has_join_values?
joins_values.any? || left_outer_joins_values.any?
end
def exec_queries(&block) def exec_queries(&block)
@records = eager_loading? ? find_with_associations.freeze : @klass.find_by_sql(arel, bound_attributes, &block).freeze @records = eager_loading? ? find_with_associations.freeze : @klass.find_by_sql(arel, bound_attributes, &block).freeze

View file

@ -90,6 +90,14 @@ class PersistenceTest < ActiveRecord::TestCase
assert_equal count, Pet.joins(:toys).where(where_args).delete_all assert_equal count, Pet.joins(:toys).where(where_args).delete_all
end end
def test_delete_all_with_left_joins
where_args = { toys: { name: "Bone" } }
count = Pet.left_joins(:toys).where(where_args).count
assert_equal count, 1
assert_equal count, Pet.left_joins(:toys).where(where_args).delete_all
end
def test_delete_all_with_joins_and_where_part_is_not_hash def test_delete_all_with_joins_and_where_part_is_not_hash
where_args = ["toys.name = ?", "Bone"] where_args = ["toys.name = ?", "Bone"]
count = Pet.joins(:toys).where(where_args).count count = Pet.joins(:toys).where(where_args).count
@ -453,6 +461,20 @@ class PersistenceTest < ActiveRecord::TestCase
assert_nil Topic.find(2).last_read assert_nil Topic.find(2).last_read
end end
def test_update_all_with_joins
where_args = { toys: { name: "Bone" } }
count = Pet.left_joins(:toys).where(where_args).count
assert_equal count, Pet.joins(:toys).where(where_args).update_all(name: "Bob")
end
def test_update_all_with_left_joins
where_args = { toys: { name: "Bone" } }
count = Pet.left_joins(:toys).where(where_args).count
assert_equal count, Pet.left_joins(:toys).where(where_args).update_all(name: "Bob")
end
def test_update_all_with_non_standard_table_name def test_update_all_with_non_standard_table_name
assert_equal 1, WarehouseThing.where(id: 1).update_all(["value = ?", 0]) assert_equal 1, WarehouseThing.where(id: 1).update_all(["value = ?", 0])
assert_equal 0, WarehouseThing.find(1).value assert_equal 0, WarehouseThing.find(1).value