From 09f1595cbaf6ff0c148852158ed9fdf5bb226ac0 Mon Sep 17 00:00:00 2001 From: =?utf-8?q?=D0=9F=D0=B0=D0=B2=D0=B5=D0=BB=20=D0=93=D0=BE=D0=BB=D1=83=D0=B1=D0=B5=D0=B2?= Date: Fri, 20 Jun 2008 18:04:07 +0400 Subject: [PATCH] Closes #447 --- .../associations/association_collection.rb | 1 + .../associations/has_many_associations_test.rb | 7 +++++++ .../has_many_through_associations_test.rb | 8 +++++--- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 52d2a98..a3969c1 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -95,6 +95,7 @@ module ActiveRecord @owner.transaction do flatten_deeper(records).each do |record| raise_on_type_mismatch(record) + record.reload if record.respond_to?(:new_record?) && record.respond_to?(:reload) && !record.new_record? add_record_to_target_with_callbacks(record) do |r| result &&= insert_record(record) unless @owner.new_record? end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index b638143..5f04b32 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -19,6 +19,13 @@ class HasManyAssociationsTest < ActiveRecord::TestCase def setup Client.destroyed_client_ids.clear end + + def test_object_changing_existing_owner + comment = Comment.create!(:post => posts(:welcome), :body => "test body", :type => "Comment") + posts(:thinking).comments << comment + assert_equal comment.post_id, posts(:thinking).id + assert_equal comment.post, posts(:thinking) + end def force_signal37_to_load_all_clients_of_firm companies(:first_firm).clients_of_firm.each {|f| } diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index be5170f..1440b34 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -9,7 +9,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase def test_associate_existing assert_queries(2) { posts(:thinking);people(:david) } - assert_queries(1) do + assert_queries(2) do posts(:thinking).people << people(:david) end @@ -86,7 +86,8 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase # 1 query to delete the existing reader (michael) # 1 query to associate the new reader (david) - assert_queries(2) do + # 1 query to reload record not to lose existing owner (golubeff) + assert_queries(3) do posts(:welcome).people = [people(:david)] end @@ -103,8 +104,9 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert_queries(1) { posts(:thinking) } # 1 query for the new record, 1 for the join table record + # 1 query to reload record not to lose existing owner (golubeff) # No need to update the actual collection yet! - assert_queries(2) do + assert_queries(3) do posts(:thinking).people.create(:first_name=>"Jeb") end -- 1.5.3.1