Skip to content

Commit

Permalink
Merge pull request #12135 from dylanahsmith/avoid_empty_transaction
Browse files Browse the repository at this point in the history
Avoid empty transaction from setting has_one association on new record.

Conflicts:
	activerecord/CHANGELOG.md

Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information
rafaelfranca committed Sep 11, 2013
1 parent 463f989 commit 9ec8f17
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,5 +1,10 @@
## unreleased ##

* Setting a has_one association on a new record no longer causes an empty
transaction.

*Dylan Thacker-Smith*

* Fix `AR::Relation#merge` sometimes failing to preserve `readonly(false)` flag.

*thedarkone*
Expand Down
Expand Up @@ -27,14 +27,16 @@ def replace(record, save = true)

return self.target if !(target || record)
if (target != record) || record.changed?
save &&= owner.persisted?

transaction_if(save) do
remove_target!(options[:dependent]) if target && !target.destroyed?

if record
set_owner_attributes(record)
set_inverse_instance(record)

if owner.persisted? && save && !record.save
if save && !record.save
nullify_owner_attributes(record)
set_owner_attributes(target) if target
raise RecordNotSaved, "Failed to save the new associated #{reflection.name}."
Expand Down
Expand Up @@ -521,6 +521,8 @@ def test_has_one_transaction
assert_no_queries { company.account = nil }
account = Account.find(2)
assert_queries { company.account = account }

assert_no_queries { Firm.new.account = account }
end

def test_has_one_assignment_triggers_save_on_change
Expand Down

0 comments on commit 9ec8f17

Please sign in to comment.