Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Don't add non-new records back to the target array after loading targ…

…ets on associations, as that makes destroy_all destroy any created records that don't match the scope destroy_all is called on

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information...
commit 0fee3592786da833bcfbe20afc3a9483118c3c12 1 parent e0eb8e9
@willbryant willbryant authored NZKoz committed
View
2  activerecord/lib/active_record/associations/association_collection.rb
@@ -361,7 +361,7 @@ def load_target
else
f
end
- end + @target
+ end + @target.find_all {|t| t.new_record?}
else
@target = find_target
end
View
11 activerecord/test/cases/associations/has_many_associations_test.rb
@@ -866,6 +866,17 @@ def test_destroy_all
assert companies(:first_firm).clients_of_firm(true).empty?, "37signals has no clients after destroy all and refresh"
end
+ def test_destroy_all_with_creates_and_scope_that_doesnt_match_created_records
+ company = companies(:first_firm)
+ unloaded_client_matching_scope = companies(:second_client)
+ created_client_matching_scope = company.clients_of_firm.create!(:name => "Somesoft")
+ created_client_not_matching_scope = company.clients_of_firm.create!(:name => "OtherCo")
+ destroyed = company.clients_of_firm.with_oft_in_name.destroy_all
+ assert destroyed.include?(unloaded_client_matching_scope), "unloaded clients matching the scope destroy_all on should have been destroyed"
+ assert destroyed.include?(created_client_matching_scope), "loaded clients matching the scope destroy_all on should have been destroyed"
+ assert !destroyed.include?(created_client_not_matching_scope), "loaded clients not matching the scope destroy_all on should not have been destroyed"
+ end
+
def test_dependence
firm = companies(:first_firm)
assert_equal 2, firm.clients.size
View
2  activerecord/test/models/company.rb
@@ -12,6 +12,8 @@ class Company < AbstractCompany
has_many :contracts
has_many :developers, :through => :contracts
+ named_scope :with_oft_in_name, :conditions => "name LIKE '%oft%'"
+
def arbitrary_method
"I am Jack's profound disappointment"
end

0 comments on commit 0fee359

Please sign in to comment.
Something went wrong with that request. Please try again.