aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2017-12-01 11:49:36 +0900
committerRyuta Kamizono <kamipo@gmail.com>2017-12-01 12:49:46 +0900
commit0dc3b6535dfb8127f568bb4abc2fb09118ce4b96 (patch)
treeef8e9543dd355489af3ff8ecc668dcecd99efdbc /activerecord
parent81b2aed360221a2cb5419cc1e5fd697961a183a6 (diff)
downloadrails-0dc3b6535dfb8127f568bb4abc2fb09118ce4b96.tar.gz
rails-0dc3b6535dfb8127f568bb4abc2fb09118ce4b96.tar.bz2
rails-0dc3b6535dfb8127f568bb4abc2fb09118ce4b96.zip
Maintain raising `RecordNotFound` for class level `update` and` destroy`
In 35836019, class level `update` and `destroy` suppressed `RecordNotFound` to ensure returning affected objects. But `RecordNotFound` is a common exception caught by a `rescue_from` handler. So changing the behavior when a typical `params[:id]` is passed has a compatibility problem. The previous behavior should not be changed. Fixes #31301.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/persistence.rb13
-rw-r--r--activerecord/test/cases/persistence_test.rb14
2 files changed, 21 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb
index 4e1b05dbf6..17459f2505 100644
--- a/activerecord/lib/active_record/persistence.rb
+++ b/activerecord/lib/active_record/persistence.rb
@@ -99,7 +99,11 @@ 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| update(one_id, attributes[idx]) }.compact
+ id.map.with_index { |one_id, idx|
+ object = find_by(primary_key => one_id)
+ object.update(attributes[idx]) if object
+ object
+ }.compact
elsif id == :all
all.each { |record| record.update(attributes) }
else
@@ -112,7 +116,6 @@ module ActiveRecord
object.update(attributes)
object
end
- rescue RecordNotFound
end
# Destroy an object (or multiple objects) that has the given id. The object is instantiated first,
@@ -136,11 +139,13 @@ module ActiveRecord
# Todo.destroy(todos)
def destroy(id)
if id.is_a?(Array)
- id.map { |one_id| destroy(one_id) }.compact
+ id.map { |one_id|
+ object = find_by(primary_key => one_id)
+ object.destroy if object
+ }.compact
else
find(id).destroy
end
- rescue RecordNotFound
end
# Deletes the row with a primary key matching the +id+ argument, using a
diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb
index f088c064f5..643032e7cf 100644
--- a/activerecord/test/cases/persistence_test.rb
+++ b/activerecord/test/cases/persistence_test.rb
@@ -473,10 +473,18 @@ class PersistenceTest < ActiveRecord::TestCase
assert_raise(ActiveRecord::RecordNotFound) { Topic.find(topic.id) }
end
- def test_record_not_found_exception
+ def test_find_raises_record_not_found_exception
assert_raise(ActiveRecord::RecordNotFound) { Topic.find(99999) }
end
+ def test_update_raises_record_not_found_exception
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.update(99999, approved: true) }
+ end
+
+ def test_destroy_raises_record_not_found_exception
+ assert_raise(ActiveRecord::RecordNotFound) { Topic.destroy(99999) }
+ end
+
def test_update_all
assert_equal Topic.count, Topic.update_all("content = 'bulk updated!'")
assert_equal "bulk updated!", Topic.find(1).content
@@ -938,7 +946,9 @@ class PersistenceTest < ActiveRecord::TestCase
should_not_be_destroyed_reply = Reply.create("title" => "hello", "content" => "world")
Topic.find(1).replies << should_not_be_destroyed_reply
- assert_nil Topic.where("1=0").scoping { Topic.destroy(1) }
+ assert_raise(ActiveRecord::RecordNotFound) do
+ Topic.where("1=0").scoping { Topic.destroy(1) }
+ end
assert_nothing_raised { Topic.find(1) }
assert_nothing_raised { Reply.find(should_not_be_destroyed_reply.id) }