aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2007-05-24 20:39:51 +0000
committerRick Olson <technoweenie@gmail.com>2007-05-24 20:39:51 +0000
commitc57c7210cd9216a72da8e9c7321d416ebfe1391e (patch)
tree8de460ee2f1125d8bbfe39d85a4166cadf7f6670 /activerecord/test
parent4b9e67c5b9d9f751d633c787d24a1491f3ef498e (diff)
downloadrails-c57c7210cd9216a72da8e9c7321d416ebfe1391e.tar.gz
rails-c57c7210cd9216a72da8e9c7321d416ebfe1391e.tar.bz2
rails-c57c7210cd9216a72da8e9c7321d416ebfe1391e.zip
Ensure that associations with :dependent => :delete_all respect :conditions option. Closes #8034 [danger, joshpeek, Rick]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6827 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rwxr-xr-xactiverecord/test/associations_test.rb24
-rwxr-xr-xactiverecord/test/fixtures/company.rb2
2 files changed, 26 insertions, 0 deletions
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index 03c344de7c..0dad144199 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -844,6 +844,30 @@ class HasManyAssociationsTest < Test::Unit::TestCase
assert Client.find_by_id(client_id).nil?
end
+ def test_dependent_association_respects_optional_conditions_on_delete
+ firm = companies(:odegy)
+ Client.create(:client_of => firm.id, :name => "BigShot Inc.")
+ Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
+ # only one of two clients is included in the association due to the :conditions key
+ assert_equal 2, Client.find_all_by_client_of(firm.id).size
+ assert_equal 1, firm.dependent_conditional_clients_of_firm.size
+ firm.destroy
+ # only the correctly associated client should have been deleted
+ assert_equal 1, Client.find_all_by_client_of(firm.id).size
+ end
+
+ def test_dependent_association_respects_optional_sanitized_conditions_on_delete
+ firm = companies(:odegy)
+ Client.create(:client_of => firm.id, :name => "BigShot Inc.")
+ Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
+ # only one of two clients is included in the association due to the :conditions key
+ assert_equal 2, Client.find_all_by_client_of(firm.id).size
+ assert_equal 1, firm.dependent_sanitized_conditional_clients_of_firm.size
+ firm.destroy
+ # only the correctly associated client should have been deleted
+ assert_equal 1, Client.find_all_by_client_of(firm.id).size
+ end
+
def test_clearing_without_initial_access
firm = companies(:first_firm)
diff --git a/activerecord/test/fixtures/company.rb b/activerecord/test/fixtures/company.rb
index ff92b14fcb..0c93c80e03 100755
--- a/activerecord/test/fixtures/company.rb
+++ b/activerecord/test/fixtures/company.rb
@@ -50,6 +50,8 @@ end
class ExclusivelyDependentFirm < Company
has_one :account, :foreign_key => "firm_id", :dependent => :delete
+ has_many :dependent_sanitized_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => "name = 'BigShot Inc.'"
+ has_many :dependent_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => ["name = ?", 'BigShot Inc.']
end
class Client < Company