From 505bf59457558b8841013d147f13737e13e00ee7 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Sat, 10 Nov 2012 05:41:58 -0800 Subject: Merge pull request #8154 from bogdan/has_one_association_performance Remove unwanted transaction when has one association is built Conflicts: activerecord/CHANGELOG.md --- .../lib/active_record/associations/has_one_association.rb | 10 +++++++++- 1 file changed, 9 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 501ebe7c5b..56f9013d61 100644 --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -11,7 +11,7 @@ module ActiveRecord # 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 + transaction_if(save) do remove_target!(options[:dependent]) if target && !target.destroyed? if record @@ -70,6 +70,14 @@ module ActiveRecord def nullify_owner_attributes(record) record[reflection.foreign_key] = nil end + + def transaction_if(value) + if value + reflection.klass.transaction { yield } + else + yield + end + end end end end -- cgit v1.2.3