Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #12135 from dylanahsmith/avoid_empty_transaction

Avoid empty transaction from setting has_one association on new record.

Conflicts:
	activerecord/CHANGELOG.md
  • Loading branch information...
commit 1fa76a2f3de5276001afe5d6815136680f7a87de 2 parents 75af032 + a94e2db
@rafaelfranca rafaelfranca authored
View
5 activerecord/CHANGELOG.md
@@ -1,3 +1,8 @@
+* 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*
View
4 activerecord/lib/active_record/associations/has_one_association.rb
@@ -27,6 +27,8 @@ 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?
@@ -34,7 +36,7 @@ def replace(record, save = true)
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}."
View
2  activerecord/test/cases/associations/has_one_associations_test.rb
@@ -505,6 +505,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
Please sign in to comment.
Something went wrong with that request. Please try again.