From a94e2db05ccd0d4ae7681d26b4ce929184cffefc Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Wed, 4 Sep 2013 18:36:28 -0400 Subject: Avoid empty transaction from setting has_one association on new record. --- activerecord/lib/active_record/associations/has_one_association.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/associations') diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb index 3ab1ea1ff4..1dc98ec640 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -27,6 +27,7 @@ module ActiveRecord 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 +35,7 @@ module ActiveRecord 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}." -- cgit v1.2.3