aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2009-12-29 11:36:40 +0530
committerPratik Naik <pratiknaik@gmail.com>2009-12-29 11:46:07 +0530
commit54b80c73611fe8eb19039381bf0322326f5e1b51 (patch)
tree701a8e76da825bc839cf0cde241c4ff994633273 /activerecord
parentf79caa49fb78bca986a4321830c4cd155c17e13a (diff)
downloadrails-54b80c73611fe8eb19039381bf0322326f5e1b51.tar.gz
rails-54b80c73611fe8eb19039381bf0322326f5e1b51.tar.bz2
rails-54b80c73611fe8eb19039381bf0322326f5e1b51.zip
Add Relation#delete_all
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG4
-rw-r--r--activerecord/lib/active_record/relation.rb6
-rw-r--r--activerecord/test/cases/relations_test.rb20
3 files changed, 30 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 28ae2262e2..4846774deb 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,9 @@
*Edge*
+* Add Relation#delete_all. [Pratik Naik]
+
+ Item.where(:colour => 'red').delete_all
+
* Add Model.having and Relation#having. [Pratik Naik]
Developer.group("salary").having("sum(salary) > 10000").select("salary")
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index e495aa80db..a3d7be3afe 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -134,6 +134,7 @@ module ActiveRecord
if [String, Hash, Array].include?(args.first.class)
conditions = @klass.send(:merge_conditions, args.size > 1 ? Array.wrap(args) : args.first)
+ conditions = Arel::SqlLiteral.new(conditions) if conditions
else
conditions = args.first
end
@@ -223,6 +224,11 @@ module ActiveRecord
reset
end
+ def delete_all
+ @relation.delete
+ reset
+ end
+
def loaded?
@loaded
end
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index ded4f2f479..92005fc224 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -329,6 +329,26 @@ class RelationTest < ActiveRecord::TestCase
assert davids.loaded?
end
+ def test_delete_all
+ davids = Author.where(:name => 'David')
+
+ assert_difference('Author.count', -1) { davids.delete_all }
+ assert ! davids.loaded?
+ end
+
+ def test_delete_all_loaded
+ davids = Author.where(:name => 'David')
+
+ # Force load
+ assert_equal [authors(:david)], davids.to_a
+ assert davids.loaded?
+
+ assert_difference('Author.count', -1) { davids.delete_all }
+
+ assert_equal [], davids.to_a
+ assert davids.loaded?
+ end
+
def test_relation_merging
devs = Developer.where("salary >= 80000") & Developer.limit(2) & Developer.order('id ASC').where("id < 3")
assert_equal [developers(:david), developers(:jamis)], devs.to_a