Skip to content

Commit 4d7354a

Browse files
committed
Ensure that delete_all on collection proxy returns affected count
Unlike the `Relation#delete_all`, `delete_all` on collection proxy doesn't return affected count. Since the `CollectionProxy` is a subclass of the `Relation`, this inconsistency is probably not intended, so it should return the count consistently.
1 parent 6ca6478 commit 4d7354a

File tree

5 files changed

+23
-6
lines changed

5 files changed

+23
-6
lines changed

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Ensure that `delete_all` on collection proxy returns affected count.
2+
3+
*Ryuta Kamizono*
4+
15
* Add the ability to prevent writes to a database for the duration of a block.
26

37
Allows the application to prevent writes to a database. This can be useful when

activerecord/lib/active_record/associations/has_many_association.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ def delete_count(method, scope)
9999
def delete_or_nullify_all_records(method)
100100
count = delete_count(method, scope)
101101
update_counter(-count)
102+
count
102103
end
103104

104105
# Deletes the records according to the <tt>:dependent</tt> option.

activerecord/lib/active_record/associations/has_many_through_association.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ def delete_records(records, method)
161161
else
162162
update_counter(-count)
163163
end
164+
165+
count
164166
end
165167

166168
def difference(a, b)

activerecord/test/cases/associations/has_many_associations_test.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def test_do_not_call_callbacks_for_delete_all
264264
car = Car.create(name: "honda")
265265
car.funky_bulbs.create!
266266
assert_equal 1, car.funky_bulbs.count
267-
assert_nothing_raised { car.reload.funky_bulbs.delete_all }
267+
assert_equal 1, car.reload.funky_bulbs.delete_all
268268
assert_equal 0, car.funky_bulbs.count, "bulbs should have been deleted using :delete_all strategy"
269269
end
270270

@@ -1405,7 +1405,7 @@ def test_delete_all
14051405
assert_equal 3, clients.count
14061406

14071407
assert_difference "Client.count", -(clients.count) do
1408-
companies(:first_firm).dependent_clients_of_firm.delete_all
1408+
assert_equal clients.count, companies(:first_firm).dependent_clients_of_firm.delete_all
14091409
end
14101410
end
14111411

@@ -1502,10 +1502,20 @@ def test_clearing_a_dependent_association_collection
15021502
def test_delete_all_with_option_delete_all
15031503
firm = companies(:first_firm)
15041504
client_id = firm.dependent_clients_of_firm.first.id
1505-
firm.dependent_clients_of_firm.delete_all(:delete_all)
1505+
count = firm.dependent_clients_of_firm.count
1506+
assert_equal count, firm.dependent_clients_of_firm.delete_all(:delete_all)
15061507
assert_nil Client.find_by_id(client_id)
15071508
end
15081509

1510+
def test_delete_all_with_option_nullify
1511+
firm = companies(:first_firm)
1512+
client_id = firm.dependent_clients_of_firm.first.id
1513+
count = firm.dependent_clients_of_firm.count
1514+
assert_equal firm, Client.find(client_id).firm
1515+
assert_equal count, firm.dependent_clients_of_firm.delete_all(:nullify)
1516+
assert_nil Client.find(client_id).firm
1517+
end
1518+
15091519
def test_delete_all_accepts_limited_parameters
15101520
firm = companies(:first_firm)
15111521
assert_raise(ArgumentError) do

activerecord/test/cases/associations/has_many_through_associations_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def test_delete_all_for_with_dependent_option_destroy
200200

201201
assert_no_difference "Job.count" do
202202
assert_difference "Reference.count", -1 do
203-
person.reload.jobs_with_dependent_destroy.delete_all
203+
assert_equal 1, person.reload.jobs_with_dependent_destroy.delete_all
204204
end
205205
end
206206
end
@@ -211,7 +211,7 @@ def test_delete_all_for_with_dependent_option_nullify
211211

212212
assert_no_difference "Job.count" do
213213
assert_no_difference "Reference.count" do
214-
person.reload.jobs_with_dependent_nullify.delete_all
214+
assert_equal 1, person.reload.jobs_with_dependent_nullify.delete_all
215215
end
216216
end
217217
end
@@ -222,7 +222,7 @@ def test_delete_all_for_with_dependent_option_delete_all
222222

223223
assert_no_difference "Job.count" do
224224
assert_difference "Reference.count", -1 do
225-
person.reload.jobs_with_dependent_delete_all.delete_all
225+
assert_equal 1, person.reload.jobs_with_dependent_delete_all.delete_all
226226
end
227227
end
228228
end

0 commit comments

Comments
 (0)