Skip to content

Commit 5e1d07d

Browse files
committed
Move ActiveRecord::Base.delete in Relation
Ref: rails#50396
1 parent 135af7b commit 5e1d07d

File tree

5 files changed

+36
-32
lines changed

5 files changed

+36
-32
lines changed

activerecord/lib/active_record/persistence.rb

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -544,30 +544,6 @@ def destroy(id)
544544
end
545545
end
546546

547-
# Deletes the row with a primary key matching the +id+ argument, using an
548-
# SQL +DELETE+ statement, and returns the number of rows deleted. Active
549-
# Record objects are not instantiated, so the object's callbacks are not
550-
# executed, including any <tt>:dependent</tt> association options.
551-
#
552-
# You can delete multiple rows at once by passing an Array of <tt>id</tt>s.
553-
#
554-
# Note: Although it is often much faster than the alternative, #destroy,
555-
# skipping callbacks might bypass business logic in your application
556-
# that ensures referential integrity or performs other essential jobs.
557-
#
558-
# ==== Examples
559-
#
560-
# # Delete a single row
561-
# Todo.delete(1)
562-
#
563-
# # Delete multiple rows
564-
# Todo.delete([2,3,4])
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-
568-
delete_by(primary_key => id_or_array)
569-
end
570-
571547
def _insert_record(connection, values, returning) # :nodoc:
572548
primary_key = self.primary_key
573549
primary_key_value = nil

activerecord/lib/active_record/querying.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Querying
1010
:first_or_create, :first_or_create!, :first_or_initialize,
1111
:find_or_create_by, :find_or_create_by!, :find_or_initialize_by,
1212
:create_or_find_by, :create_or_find_by!,
13-
:destroy_all, :delete_all, :update_all, :touch_all, :destroy_by, :delete_by,
13+
:destroy_all, :delete, :delete_all, :update_all, :touch_all, :destroy_by, :delete_by,
1414
:find_each, :find_in_batches, :in_batches,
1515
:select, :reselect, :order, :regroup, :in_order_of, :reorder, :group, :limit, :offset, :joins, :left_joins, :left_outer_joins,
1616
:where, :rewhere, :invert_where, :preload, :extract_associated, :eager_load, :includes, :from, :lock, :readonly,

activerecord/lib/active_record/relation.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,30 @@ def delete_all
752752
end
753753
end
754754

755+
# Deletes the row with a primary key matching the +id+ argument, using an
756+
# SQL +DELETE+ statement, and returns the number of rows deleted. Active
757+
# Record objects are not instantiated, so the object's callbacks are not
758+
# executed, including any <tt>:dependent</tt> association options.
759+
#
760+
# You can delete multiple rows at once by passing an Array of <tt>id</tt>s.
761+
#
762+
# Note: Although it is often much faster than the alternative, #destroy,
763+
# skipping callbacks might bypass business logic in your application
764+
# that ensures referential integrity or performs other essential jobs.
765+
#
766+
# ==== Examples
767+
#
768+
# # Delete a single row
769+
# Todo.delete(1)
770+
#
771+
# # Delete multiple rows
772+
# Todo.delete([2,3,4])
773+
def delete(id_or_array)
774+
return 0 if id_or_array.nil? || (id_or_array.is_a?(Array) && id_or_array.empty?)
775+
776+
where(model.primary_key => id_or_array).delete_all
777+
end
778+
755779
# Finds and destroys all records matching the specified conditions.
756780
# This is short-hand for <tt>relation.where(condition).destroy_all</tt>.
757781
# Returns the collection of objects that were destroyed.

activerecord/test/cases/relation/delegation_test.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class QueryingMethodsDelegationTest < ActiveRecord::TestCase
6767
:first_or_create, :first_or_create!, :first_or_initialize,
6868
:find_or_create_by, :find_or_create_by!, :find_or_initialize_by,
6969
:create_or_find_by, :create_or_find_by!,
70-
:destroy_all, :delete_all, :update_all, :touch_all, :delete_by, :destroy_by
70+
:destroy_all, :delete, :delete_all, :update_all, :touch_all, :delete_by, :destroy_by
7171
]
7272

7373
def test_delegate_querying_methods
@@ -89,14 +89,14 @@ class DelegationCachingTest < ActiveRecord::TestCase
8989

9090
test "delegation doesn't override methods defined in other relation subclasses" do
9191
# precondition, some methods are available on ActiveRecord::Relation subclasses
92-
# but not ActiveRecord::Relation itself. Here `delete` is just an example.
93-
assert_equal false, ActiveRecord::Relation.method_defined?(:delete)
94-
assert_equal true, ActiveRecord::Associations::CollectionProxy.method_defined?(:delete)
92+
# but not ActiveRecord::Relation itself. Here `target` is just an example.
93+
assert_equal false, ActiveRecord::Relation.method_defined?(:target)
94+
assert_equal true, ActiveRecord::Associations::CollectionProxy.method_defined?(:target)
9595

9696
project = projects(:active_record)
97-
original_owner = project.developers_with_callbacks.method(:delete).owner
98-
Developer.all.delete(12345)
99-
assert_equal original_owner, project.developers_with_callbacks.method(:delete).owner
97+
original_owner = project.developers_with_callbacks.method(:target).owner
98+
assert_equal :__target__, Developer.all.target
99+
assert_equal original_owner, project.developers_with_callbacks.method(:target).owner
100100
end
101101
end
102102
end

activerecord/test/models/developer.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ def find_most_recent
3030
end
3131
end
3232

33+
def self.target
34+
:__target__ # Used by delegation_test.rb
35+
end
36+
3337
belongs_to :mentor
3438
belongs_to :strict_loading_mentor, strict_loading: true, foreign_key: :mentor_id, class_name: "Mentor"
3539
belongs_to :strict_loading_off_mentor, strict_loading: false, foreign_key: :mentor_id, class_name: "Mentor"

0 commit comments

Comments
 (0)