Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix delete_all when chained with joins #5202

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion activerecord/lib/active_record/relation.rb
Expand Up @@ -408,7 +408,13 @@ def delete_all(conditions = nil)
if conditions if conditions
where(conditions).delete_all where(conditions).delete_all
else else
statement = arel.compile_delete statement = if joins_values.present?
pk = "#{@klass.quoted_table_name}.#{@klass.quoted_primary_key}"
ids = select(pk)
unscoped { @klass.where("#{pk} IN (SELECT * FROM (#{ids.to_sql}) AS derived_#{table_name}_#{primary_key}_ids)").build_arel.compile_delete }
else
arel.compile_delete
end
affected = @klass.connection.delete(statement, 'SQL', bind_values) affected = @klass.connection.delete(statement, 'SQL', bind_values)


reset reset
Expand Down
20 changes: 19 additions & 1 deletion activerecord/test/cases/persistence_test.rb
Expand Up @@ -13,12 +13,14 @@
require 'models/parrot' require 'models/parrot'
require 'models/minivan' require 'models/minivan'
require 'models/person' require 'models/person'
require 'models/pet'
require 'models/toy'
require 'rexml/document' require 'rexml/document'
require 'active_support/core_ext/exception' require 'active_support/core_ext/exception'


class PersistencesTest < ActiveRecord::TestCase class PersistencesTest < ActiveRecord::TestCase


fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things', :authors, :categorizations, :categories, :posts, :minivans fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things', :authors, :categorizations, :categories, :posts, :minivans, :pets, :toys


# Oracle UPDATE does not support ORDER BY # Oracle UPDATE does not support ORDER BY
unless current_adapter?(:OracleAdapter) unless current_adapter?(:OracleAdapter)
Expand Down Expand Up @@ -76,6 +78,22 @@ def test_delete_all
assert_equal Topic.count, Topic.delete_all assert_equal Topic.count, Topic.delete_all
end end


def test_delete_all_with_joins_and_where_part_is_hash
where_args = {:toys => {:name => 'Bone'}}
count = Pet.joins(:toys).where(where_args).count

assert_equal count, 1
assert_equal count, Pet.joins(:toys).where(where_args).delete_all
end

def test_delete_all_with_joins_and_where_part_is_not_hash
where_args = ['toys.name = ?', 'Bone']
count = Pet.joins(:toys).where(where_args).count

assert_equal count, 1
assert_equal count, Pet.joins(:toys).where(where_args).delete_all
end

def test_update_by_condition def test_update_by_condition
Topic.update_all "content = 'bulk updated!'", ["approved = ?", true] Topic.update_all "content = 'bulk updated!'", ["approved = ?", true]
assert_equal "Have a nice day", Topic.find(1).content assert_equal "Have a nice day", Topic.find(1).content
Expand Down