aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG5
-rwxr-xr-xactiverecord/lib/active_record/base.rb2
-rw-r--r--activerecord/lib/active_record/relation.rb12
-rw-r--r--activerecord/test/cases/relations_test.rb13
4 files changed, 30 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 3c60bf4f21..4daeb6a302 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,10 @@
*Edge*
+* Add relation.destroy_all [Pratik Naik]
+
+ old_items = Item.where("age > 100")
+ old_items.destroy_all
+
* Add relation.exists? [Pratik Naik]
red_items = Item.where(:colours => 'red')
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index a3b123c463..cad75f80e5 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -914,7 +914,7 @@ module ActiveRecord #:nodoc:
# Person.destroy_all("last_login < '2004-04-04'")
# Person.destroy_all(:status => "inactive")
def destroy_all(conditions = nil)
- where(conditions).each {|object| object.destroy }
+ where(conditions).destroy_all
end
# Deletes the records matching +conditions+ without instantiating the records first, and hence not
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index bec9a88297..4aa460a601 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -175,13 +175,23 @@ module ActiveRecord
end
end
+ def destroy_all
+ to_a.each {|object| object.destroy}
+ reset
+ end
+
def loaded?
@loaded
end
def reload
@loaded = false
- @records = @first = @last = nil
+ reset
+ end
+
+ def reset
+ @first = @last = nil
+ @records = []
self
end
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 37bd755a5c..2ec8c0d3a2 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -315,4 +315,17 @@ class RelationTest < ActiveRecord::TestCase
assert_equal authors(:mary), authors.last
end
+ def test_destroy_all
+ davids = Author.where(:name => 'David')
+
+ # Force load
+ assert_equal [authors(:david)], davids.to_a
+ assert davids.loaded?
+
+ assert_difference('Author.count', -1) { davids.destroy_all }
+
+ assert_equal [], davids.to_a
+ assert davids.loaded?
+ end
+
end