Permalink
Browse files

Avoid empty transaction from setting has_one association on new record.

  • Loading branch information...
dylanahsmith committed Sep 4, 2013
1 parent 233c6d4 commit a94e2db05ccd0d4ae7681d26b4ce929184cffefc
@@ -1,3 +1,8 @@
+* Setting a has_one association on a new record no longer causes an empty
+ transaction.
+
+ *Dylan Thacker-Smith*
+
* Re-use `order` argument pre-processing for `reorder`.
*Paul Nikitochkin*
@@ -27,14 +27,15 @@ 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}."
@@ -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

0 comments on commit a94e2db

Please sign in to comment.