Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

AssociationCollection should not do transaction if @owner.new_record?

#3413

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,

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

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Rohit Arondekar
    Rohit Arondekar
    • 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.

    October 6th, 2010 @ 06:37 AM