From 54b80c73611fe8eb19039381bf0322326f5e1b51 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Tue, 29 Dec 2009 11:36:40 +0530 Subject: Add Relation#delete_all --- activerecord/CHANGELOG | 4 ++++ activerecord/lib/active_record/relation.rb | 6 ++++++ activerecord/test/cases/relations_test.rb | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+) 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 -- cgit v1.2.3