mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Add relation.destroy_all
This commit is contained in:
@@ -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')
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user