aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/persistence.rb
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2017-12-01 18:21:21 +0900
committerGitHub <noreply@github.com>2017-12-01 18:21:21 +0900
commit695ec1fef0b7fdb3125a3e2e4364a7f449265c1b (patch)
tree5021d6f0e85dcc26f78f11d1192ec388f31920e8 /activerecord/lib/active_record/persistence.rb
parentd041a1dcbaced9f44ed976a48312c9f492fffe5e (diff)
downloadrails-695ec1fef0b7fdb3125a3e2e4364a7f449265c1b.tar.gz
rails-695ec1fef0b7fdb3125a3e2e4364a7f449265c1b.tar.bz2
rails-695ec1fef0b7fdb3125a3e2e4364a7f449265c1b.zip
Class level `update` and `destroy` checks all the records exist before making changes (#31306)
It makes more sense than ignoring invalid IDs.
Diffstat (limited to 'activerecord/lib/active_record/persistence.rb')
-rw-r--r--activerecord/lib/active_record/persistence.rb13
1 files changed, 4 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb
index 17459f2505..a13b0d0181 100644
--- a/activerecord/lib/active_record/persistence.rb
+++ b/activerecord/lib/active_record/persistence.rb
@@ -99,11 +99,9 @@ module ActiveRecord
# for updating all records in a single query.
def update(id = :all, attributes)
if id.is_a?(Array)
- id.map.with_index { |one_id, idx|
- object = find_by(primary_key => one_id)
- object.update(attributes[idx]) if object
- object
- }.compact
+ id.map { |one_id| find(one_id) }.each_with_index { |object, idx|
+ object.update(attributes[idx])
+ }
elsif id == :all
all.each { |record| record.update(attributes) }
else
@@ -139,10 +137,7 @@ module ActiveRecord
# Todo.destroy(todos)
def destroy(id)
if id.is_a?(Array)
- id.map { |one_id|
- object = find_by(primary_key => one_id)
- object.destroy if object
- }.compact
+ find(id).each(&:destroy)
else
find(id).destroy
end