1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Persistence#delete should not be affected by scoping

`self.class.delete` is delegated to `all` and `all` is affected by
scoping. It should use `unscoped` to not be affected by that.
This commit is contained in:
Ryuta Kamizono 2017-07-16 22:34:51 +09:00
parent 8acca896cb
commit 972f0d6cb6
2 changed files with 12 additions and 1 deletions

View file

@ -175,7 +175,7 @@ module ActiveRecord
# callbacks or any <tt>:dependent</tt> association # callbacks or any <tt>:dependent</tt> association
# options, use <tt>#destroy</tt>. # options, use <tt>#destroy</tt>.
def delete def delete
self.class.delete(id) if persisted? _relation_for_itself.delete_all if persisted?
@destroyed = true @destroyed = true
freeze freeze
end end
@ -555,6 +555,10 @@ module ActiveRecord
end end
def relation_for_destroy def relation_for_destroy
_relation_for_itself
end
def _relation_for_itself
self.class.unscoped.where(self.class.primary_key => id) self.class.unscoped.where(self.class.primary_key => id)
end end

View file

@ -437,6 +437,13 @@ class PersistenceTest < ActiveRecord::TestCase
assert_not_nil Topic.find(2) assert_not_nil Topic.find(2)
end end
def test_delete_isnt_affected_by_scoping
topic = Topic.find(1)
assert_difference("Topic.count", -1) do
Topic.where("1=0").scoping { topic.delete }
end
end
def test_destroy def test_destroy
topic = Topic.find(1) topic = Topic.find(1)
assert_equal topic, topic.destroy, "topic.destroy did not return self" assert_equal topic, topic.destroy, "topic.destroy did not return self"