aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/has_one_association.rb
diff options
context:
space:
mode:
authorkennyj <kennyj@gmail.com>2012-08-08 02:13:02 +0900
committerkennyj <kennyj@gmail.com>2012-08-08 02:52:55 +0900
commit42dd5d9f2976677a4bf22347f2dde1a8135dfbb4 (patch)
tree543a13b6e9f9a0e4cee65eecd14c741a70f72550 /activerecord/lib/active_record/associations/has_one_association.rb
parent485e655082c4ddbcde7788b3eac4bb8281b37d27 (diff)
downloadrails-42dd5d9f2976677a4bf22347f2dde1a8135dfbb4.tar.gz
rails-42dd5d9f2976677a4bf22347f2dde1a8135dfbb4.tar.bz2
rails-42dd5d9f2976677a4bf22347f2dde1a8135dfbb4.zip
Fix #7191. Remove unnecessary transaction when assigning has_one associations.
Diffstat (limited to 'activerecord/lib/active_record/associations/has_one_association.rb')
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb28
1 files changed, 15 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb
index 191c4e8e39..3b96ca5eb8 100644
--- a/activerecord/lib/active_record/associations/has_one_association.rb
+++ b/activerecord/lib/active_record/associations/has_one_association.rb
@@ -7,19 +7,21 @@ module ActiveRecord
raise_on_type_mismatch(record) if record
load_target
- reflection.klass.transaction do
- if target && target != record
- remove_target!(options[:dependent]) unless target.destroyed?
- end
-
- if record
- set_owner_attributes(record)
- set_inverse_instance(record)
-
- if owner.persisted? && save && !record.save
- nullify_owner_attributes(record)
- set_owner_attributes(target) if target
- raise RecordNotSaved, "Failed to save the new associated #{reflection.name}."
+ # If target and record are nil, or target is equal to record,
+ # we don't need to have transaction.
+ if (target || record) && target != record
+ reflection.klass.transaction 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
+ nullify_owner_attributes(record)
+ set_owner_attributes(target) if target
+ raise RecordNotSaved, "Failed to save the new associated #{reflection.name}."
+ end
end
end
end