aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2017-07-17 13:41:14 -0500
committerGitHub <noreply@github.com>2017-07-17 13:41:14 -0500
commit1b62f066d529452166d4b1a2c301e663e9ff397c (patch)
tree8d719cba74fbf68bd0aa0f2cc4338f0e1778bc4a
parent8acca896cbaa01f6fc51969601888a440e83d53a (diff)
parent972f0d6cb6c9d510b9b32e7f4138184154ab70eb (diff)
downloadrails-1b62f066d529452166d4b1a2c301e663e9ff397c.tar.gz
rails-1b62f066d529452166d4b1a2c301e663e9ff397c.tar.bz2
rails-1b62f066d529452166d4b1a2c301e663e9ff397c.zip
Merge pull request #29816 from kamipo/persistence_delete_should_not_be_affected_by_scoping
`Persistence#delete` should not be affected by scoping
-rw-r--r--activerecord/lib/active_record/persistence.rb6
-rw-r--r--activerecord/test/cases/persistence_test.rb7
2 files changed, 12 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb
index a9509e562a..2e6901eb0e 100644
--- a/activerecord/lib/active_record/persistence.rb
+++ b/activerecord/lib/active_record/persistence.rb
@@ -175,7 +175,7 @@ module ActiveRecord
# callbacks or any <tt>:dependent</tt> association
# options, use <tt>#destroy</tt>.
def delete
- self.class.delete(id) if persisted?
+ _relation_for_itself.delete_all if persisted?
@destroyed = true
freeze
end
@@ -555,6 +555,10 @@ module ActiveRecord
end
def relation_for_destroy
+ _relation_for_itself
+ end
+
+ def _relation_for_itself
self.class.unscoped.where(self.class.primary_key => id)
end
diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb
index 5895c51714..5830f9916a 100644
--- a/activerecord/test/cases/persistence_test.rb
+++ b/activerecord/test/cases/persistence_test.rb
@@ -437,6 +437,13 @@ class PersistenceTest < ActiveRecord::TestCase
assert_not_nil Topic.find(2)
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
topic = Topic.find(1)
assert_equal topic, topic.destroy, "topic.destroy did not return self"