Add relation.destroy_all

This commit is contained in:
Pratik Naik
2009-12-27 19:35:55 +05:30
parent 5cd8818258
commit bbdeaae2ca
4 changed files with 30 additions and 2 deletions

View File

@@ -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 File

@@ -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

View File

@@ -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

View File

@@ -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