Skip to content

Commit 8f419e4

Browse files
committed
Move ActiveRecord::Base.destroy in Relation
Ref: rails#50396
1 parent 5e1d07d commit 8f419e4

File tree

4 files changed

+36
-35
lines changed

4 files changed

+36
-35
lines changed

activerecord/lib/active_record/persistence.rb

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -511,39 +511,6 @@ def composite_query_constraints_list # :nodoc:
511511
@composite_query_constraints_list ||= query_constraints_list || Array(primary_key)
512512
end
513513

514-
# Destroy an object (or multiple objects) that has the given id. The object is instantiated first,
515-
# therefore all callbacks and filters are fired off before the object is deleted. This method is
516-
# less efficient than #delete but allows cleanup methods and other actions to be run.
517-
#
518-
# This essentially finds the object (or multiple objects) with the given id, creates a new object
519-
# from the attributes, and then calls destroy on it.
520-
#
521-
# ==== Parameters
522-
#
523-
# * +id+ - This should be the id or an array of ids to be destroyed.
524-
#
525-
# ==== Examples
526-
#
527-
# # Destroy a single object
528-
# Todo.destroy(1)
529-
#
530-
# # Destroy multiple objects
531-
# todos = [1,2,3]
532-
# Todo.destroy(todos)
533-
def destroy(id)
534-
multiple_ids = if composite_primary_key?
535-
id.first.is_a?(Array)
536-
else
537-
id.is_a?(Array)
538-
end
539-
540-
if multiple_ids
541-
find(id).each(&:destroy)
542-
else
543-
find(id).destroy
544-
end
545-
end
546-
547514
def _insert_record(connection, values, returning) # :nodoc:
548515
primary_key = self.primary_key
549516
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, :delete_all, :update_all, :touch_all, :destroy_by, :delete_by,
13+
:destroy, :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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,40 @@ def delete(id_or_array)
776776
where(model.primary_key => id_or_array).delete_all
777777
end
778778

779+
780+
# Destroy an object (or multiple objects) that has the given id. The object is instantiated first,
781+
# therefore all callbacks and filters are fired off before the object is deleted. This method is
782+
# less efficient than #delete but allows cleanup methods and other actions to be run.
783+
#
784+
# This essentially finds the object (or multiple objects) with the given id, creates a new object
785+
# from the attributes, and then calls destroy on it.
786+
#
787+
# ==== Parameters
788+
#
789+
# * +id+ - This should be the id or an array of ids to be destroyed.
790+
#
791+
# ==== Examples
792+
#
793+
# # Destroy a single object
794+
# Todo.destroy(1)
795+
#
796+
# # Destroy multiple objects
797+
# todos = [1,2,3]
798+
# Todo.destroy(todos)
799+
def destroy(id)
800+
multiple_ids = if model.composite_primary_key?
801+
id.first.is_a?(Array)
802+
else
803+
id.is_a?(Array)
804+
end
805+
806+
if multiple_ids
807+
find(id).each(&:destroy)
808+
else
809+
find(id).destroy
810+
end
811+
end
812+
779813
# Finds and destroys all records matching the specified conditions.
780814
# This is short-hand for <tt>relation.where(condition).destroy_all</tt>.
781815
# Returns the collection of objects that were destroyed.

activerecord/test/cases/relation/delegation_test.rb

Lines changed: 1 addition & 1 deletion
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, :delete_all, :update_all, :touch_all, :delete_by, :destroy_by
70+
:destroy, :destroy_all, :delete, :delete_all, :update_all, :touch_all, :delete_by, :destroy_by
7171
]
7272

7373
def test_delegate_querying_methods

0 commit comments

Comments
 (0)