Browse files

Add relation.destroy_all

  • Loading branch information...
1 parent 5cd8818 commit bbdeaae2cac37882b091474ec596ade3de3b5e7c @lifo lifo committed Dec 27, 2009
View
5 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')
View
2 activerecord/lib/active_record/base.rb
@@ -914,7 +914,7 @@ def update_all(updates, conditions = nil, options = {})
# 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
View
12 activerecord/lib/active_record/relation.rb
@@ -175,13 +175,23 @@ def last
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
View
13 activerecord/test/cases/relations_test.rb
@@ -315,4 +315,17 @@ def test_last
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

0 comments on commit bbdeaae

Please sign in to comment.