aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorDylan Thacker-Smith <Dylan.Smith@shopify.com>2013-09-04 18:36:28 -0400
committerDylan Thacker-Smith <Dylan.Smith@shopify.com>2013-09-11 14:27:27 -0400
commita94e2db05ccd0d4ae7681d26b4ce929184cffefc (patch)
tree00fdab1fb3af278c29bde03048752a7e8637c988 /activerecord/lib
parent233c6d4c298434544e349a559906f5fa1883e070 (diff)
downloadrails-a94e2db05ccd0d4ae7681d26b4ce929184cffefc.tar.gz
rails-a94e2db05ccd0d4ae7681d26b4ce929184cffefc.tar.bz2
rails-a94e2db05ccd0d4ae7681d26b4ce929184cffefc.zip
Avoid empty transaction from setting has_one association on new record.
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb3
1 files changed, 2 insertions, 1 deletions
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}."