Skip to content

Commit

Permalink
Merge pull request rails#14234 from arthurnn/fix_trans_on_replace
Browse files Browse the repository at this point in the history
Remove unnecessary db call when replacing.
  • Loading branch information
spastorino committed Mar 1, 2014
2 parents f160107 + 774160b commit f60b524
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,9 @@ def replace(other_array)
if owner.new_record?
replace_records(other_array, original_target)
else
transaction { replace_records(other_array, original_target) }
if other_array != original_target
transaction { replace_records(other_array, original_target) }
end
end
end

Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/associations/has_many_associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,16 @@ def test_replace_failure
assert_equal orig_accounts, firm.accounts
end

def test_replace_with_same_content
firm = Firm.first
firm.clients = []
firm.save

assert_queries(0, ignore_none: true) do
firm.clients = []
end
end

def test_transactions_when_replacing_on_persisted
good = Client.new(:name => "Good")
bad = Client.new(:name => "Bad", :raise_on_save => true)
Expand Down

0 comments on commit f60b524

Please sign in to comment.