Skip to content

Commit

Permalink
Don't add non-new records back to the target array after loading targ…
Browse files Browse the repository at this point in the history
…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
willbryant authored and NZKoz committed Dec 7, 2010
1 parent e0eb8e9 commit 0fee359
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions activerecord/test/cases/associations/has_many_associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions activerecord/test/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0fee359

Please sign in to comment.