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 --- activerecord/CHANGELOG.md | 9 +++++++++ .../lib/active_record/associations/has_one_association.rb | 10 +++++++++- .../test/cases/associations/has_one_associations_test.rb | 6 ++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index bcc7765d2e..8083a26b17 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,14 @@ ## Rails 3.2.10 (unreleased) +* Do not create useless database transaction when building `has_one` association. [Backport #8154] + + Example: + + User.has_one :profile + User.new.build_profile + + *Bogdan Gusiev* + * `AR::Base#attributes_before_type_cast` now returns unserialized values for serialized attributes. *Nikita Afanasenko* 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 diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index 08831a42ba..31aa3788c7 100644 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -173,6 +173,12 @@ class HasOneAssociationsTest < ActiveRecord::TestCase assert_equal account, firm.account end + def test_build_association_dont_create_transaction + assert_no_queries { + Firm.new.build_account + } + end + def test_build_and_create_should_not_happen_within_scope pirate = pirates(:blackbeard) scoped_count = pirate.association(:foo_bulb).scoped.where_values.count -- cgit v1.2.3