File tree Expand file tree Collapse file tree 2 files changed +13
-0
lines changed Expand file tree Collapse file tree 2 files changed +13
-0
lines changed Original file line number Diff line number Diff line change @@ -563,6 +563,8 @@ def destroy(id)
563
563
# # Delete multiple rows
564
564
# Todo.delete([2,3,4])
565
565
def delete ( id_or_array )
566
+ return 0 if id_or_array . nil? || ( id_or_array . is_a? ( Array ) && id_or_array . empty? )
567
+
566
568
delete_by ( primary_key => id_or_array )
567
569
end
568
570
Original file line number Diff line number Diff line change @@ -1357,6 +1357,17 @@ def test_class_level_delete
1357
1357
assert_nothing_raised { Reply . find ( should_not_be_destroyed_reply . id ) }
1358
1358
end
1359
1359
1360
+ def test_class_level_delete_with_invalid_ids
1361
+ assert_no_queries do
1362
+ assert_equal 0 , Topic . delete ( nil )
1363
+ assert_equal 0 , Topic . delete ( [ ] )
1364
+ end
1365
+
1366
+ assert_difference -> { Topic . count } , -1 do
1367
+ assert_equal 1 , Topic . delete ( topics ( :first ) . id )
1368
+ end
1369
+ end
1370
+
1360
1371
def test_class_level_delete_is_affected_by_scoping
1361
1372
should_not_be_destroyed_reply = Reply . create ( "title" => "hello" , "content" => "world" )
1362
1373
Topic . find ( 1 ) . replies << should_not_be_destroyed_reply
You can’t perform that action at this time.
0 commit comments