This project is archived and is in readonly mode.

#3413 ✓stale
Nobuhiro IMAI

AssociationCollection should not do transaction if @owner.new_record?

Reported by Nobuhiro IMAI | October 22nd, 2009 @ 05:22 PM

I found that AssociationCollection#<< do empty transaction even if the @owner is new record.

>> firm = Firm.new
>> firm.clients << Client.new
BEGIN
COMMIT

I feel that this is not necessary. How about not doing empty transaction is such case?

diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index 42b6e5d..850d0d5 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -137,6 +137,7 @@ module ActiveRecord
       #     # same effect as calling Book.transaction
       #   end
       def transaction(*args)
+        return yield if @owner.new_record?
         @reflection.klass.transaction(*args) do
           yield
         end

However, I'm not sure how to check this behavior in test. BEGIN and COMMIT are ignored
in assert_queries, and SQLite3 doesn't execute BEGIN/COMMIT AFAIK.

Thanks,

Comments and changes to this ticket

  • Rohit Arondekar

    Rohit Arondekar October 6th, 2010 @ 06:37 AM

    • State changed from “new” to “stale”
    • Importance changed from “” to “”

    Marking ticket as stale. If this is still an issue please leave a comment with suggested changes, creating a patch with tests, rebasing an existing patch or just confirming the issue on a latest release or master/branches.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages