From 650556e06ef073bb856900e1e028cb298047ac7a Mon Sep 17 00:00:00 2001 From: Justin Smestad Date: Thu, 30 Oct 2008 11:19:59 -0600 Subject: [PATCH] Implemented destroy_all with conditions on has_many association --- .../associations/has_many_association.rb | 11 +++++++++ .../associations/has_many_associations_test.rb | 23 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 0 deletions(-) diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 3348079..1b12008 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -5,6 +5,17 @@ module ActiveRecord # If the association has a :through option further specialization # is provided by its child HasManyThroughAssociation. class HasManyAssociation < AssociationCollection #:nodoc: + + # Allows destroy_all to be called with conditions on has_many associations + # while perserving the original functionality in ActiveRecord::Base + def destroy_all(conditions = nil) + result = conditions ? find(:all, :conditions => conditions) : self + transaction do + result.each { |record| record.destroy } + end + conditions ? reset : reset_target! + end + protected def owner_quoted_id if @reflection.options[:primary_key] diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 59784e1..52353b2 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -583,6 +583,29 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 0, companies(:first_firm).clients_of_firm(true).size end + def test_destroy_all + force_signal37_to_load_all_clients_of_firm + companies(:first_firm).clients_of_firm.create("name" => "Client 1") + companies(:first_firm).clients_of_firm.create("name" => "Client 2") + companies(:first_firm).clients_of_firm.create("name" => "Client 3") + companies(:first_firm).clients_of_firm.create("name" => "Company Joe") + assert_equal 5, companies(:first_firm).clients_of_firm.size + companies(:first_firm).clients_of_firm.destroy_all + assert_equal 0, companies(:first_firm).clients_of_firm.size + end + + def test_destroy_all_with_conditions + force_signal37_to_load_all_clients_of_firm + companies(:first_firm).clients_of_firm.create("name" => "Dude") + companies(:first_firm).clients_of_firm.create("name" => "Dog") + companies(:first_firm).clients_of_firm.create("name" => "Dan") + companies(:first_firm).clients_of_firm.create("name" => "Company Joe") + assert_equal 5, companies(:first_firm).clients_of_firm.size + companies(:first_firm).clients_of_firm.destroy_all("name LIKE 'D%'") + assert_equal 2, companies(:first_firm).clients_of_firm.size + assert_not_nil companies(:first_firm) + end + def test_clearing_an_association_collection firm = companies(:first_firm) client_id = firm.clients_of_firm.first.id -- 1.6.0.2