Move `ActiveRecord::Base.destroy` in `Relation`

Ref: https://github.com/rails/rails/pull/50396
This commit is contained in:
Jean Boussier 2024-05-13 12:22:06 +09:00
parent 5e1d07dff4
commit 8f419e482b
4 changed files with 36 additions and 35 deletions

View File

@ -511,39 +511,6 @@ module ActiveRecord
@composite_query_constraints_list ||= query_constraints_list || Array(primary_key)
end
# Destroy an object (or multiple objects) that has the given id. The object is instantiated first,
# therefore all callbacks and filters are fired off before the object is deleted. This method is
# less efficient than #delete but allows cleanup methods and other actions to be run.
#
# This essentially finds the object (or multiple objects) with the given id, creates a new object
# from the attributes, and then calls destroy on it.
#
# ==== Parameters
#
# * +id+ - This should be the id or an array of ids to be destroyed.
#
# ==== Examples
#
# # Destroy a single object
# Todo.destroy(1)
#
# # Destroy multiple objects
# todos = [1,2,3]
# Todo.destroy(todos)
def destroy(id)
multiple_ids = if composite_primary_key?
id.first.is_a?(Array)
else
id.is_a?(Array)
end
if multiple_ids
find(id).each(&:destroy)
else
find(id).destroy
end
end
def _insert_record(connection, values, returning) # :nodoc:
primary_key = self.primary_key
primary_key_value = nil

View File

@ -10,7 +10,7 @@ module ActiveRecord
:first_or_create, :first_or_create!, :first_or_initialize,
:find_or_create_by, :find_or_create_by!, :find_or_initialize_by,
:create_or_find_by, :create_or_find_by!,
:destroy_all, :delete, :delete_all, :update_all, :touch_all, :destroy_by, :delete_by,
:destroy, :destroy_all, :delete, :delete_all, :update_all, :touch_all, :destroy_by, :delete_by,
:find_each, :find_in_batches, :in_batches,
:select, :reselect, :order, :regroup, :in_order_of, :reorder, :group, :limit, :offset, :joins, :left_joins, :left_outer_joins,
:where, :rewhere, :invert_where, :preload, :extract_associated, :eager_load, :includes, :from, :lock, :readonly,

View File

@ -776,6 +776,40 @@ module ActiveRecord
where(model.primary_key => id_or_array).delete_all
end
# Destroy an object (or multiple objects) that has the given id. The object is instantiated first,
# therefore all callbacks and filters are fired off before the object is deleted. This method is
# less efficient than #delete but allows cleanup methods and other actions to be run.
#
# This essentially finds the object (or multiple objects) with the given id, creates a new object
# from the attributes, and then calls destroy on it.
#
# ==== Parameters
#
# * +id+ - This should be the id or an array of ids to be destroyed.
#
# ==== Examples
#
# # Destroy a single object
# Todo.destroy(1)
#
# # Destroy multiple objects
# todos = [1,2,3]
# Todo.destroy(todos)
def destroy(id)
multiple_ids = if model.composite_primary_key?
id.first.is_a?(Array)
else
id.is_a?(Array)
end
if multiple_ids
find(id).each(&:destroy)
else
find(id).destroy
end
end
# Finds and destroys all records matching the specified conditions.
# This is short-hand for <tt>relation.where(condition).destroy_all</tt>.
# Returns the collection of objects that were destroyed.

View File

@ -67,7 +67,7 @@ module ActiveRecord
:first_or_create, :first_or_create!, :first_or_initialize,
:find_or_create_by, :find_or_create_by!, :find_or_initialize_by,
:create_or_find_by, :create_or_find_by!,
:destroy_all, :delete, :delete_all, :update_all, :touch_all, :delete_by, :destroy_by
:destroy, :destroy_all, :delete, :delete_all, :update_all, :touch_all, :delete_by, :destroy_by
]
def test_delegate_querying_methods