aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorBogdan Gusiev <agresso@gmail.com>2012-11-10 15:30:20 +0200
committerBogdan Gusiev <agresso@gmail.com>2012-11-10 15:30:20 +0200
commit3cb0f3feed7130225cc169c2b1476acf0b11e066 (patch)
treed6a2504deb525cf8f4cac8deca8cfa801b58e849 /activerecord/lib/active_record
parenta002442fbce098ce5ae9597784ab2755bdad111a (diff)
downloadrails-3cb0f3feed7130225cc169c2b1476acf0b11e066.tar.gz
rails-3cb0f3feed7130225cc169c2b1476acf0b11e066.tar.bz2
rails-3cb0f3feed7130225cc169c2b1476acf0b11e066.zip
Do not create useless database transaction when building `has_one` association.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations/has_one_association.rb10
1 files changed, 9 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 06bead41de..ee816d2392 100644
--- a/activerecord/lib/active_record/associations/has_one_association.rb
+++ b/activerecord/lib/active_record/associations/has_one_association.rb
@@ -28,7 +28,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
@@ -90,6 +90,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