From c66ac2102be2d7a53872c7bb96939f616275010e Mon Sep 17 00:00:00 2001 From: Florian Weber Date: Wed, 23 Nov 2005 21:03:25 +0000 Subject: Fixed bug where using update_attribute after pushing a record to a habtm association of the object caused duplicate rows in the join table. #2888 git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3167 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/associations.rb | 6 +++++- activerecord/test/associations_test.rb | 11 ++++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index c5116d6d9e..40c0fd25ed 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed bug where using update_attribute after pushing a record to a habtm association of the object caused duplicate rows in the join table. #2888 [Florian Weber] + * MySQL, PostgreSQL: reconnect! also reconfigures the connection. Otherwise, the connection 'loses' its settings if it times out and is reconnected. #2978 [Shugo Maeda] * has_and_belongs_to_many: use JOIN instead of LEFT JOIN. [Jeremy Kemper] diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index d4b7f88907..70bc4a4d65 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -800,6 +800,7 @@ module ActiveRecord after_callback = <<-end_eval association = instance_variable_get("@#{association_name}") + if association.respond_to?(:loaded?) if @new_record_before_save records_to_save = association @@ -809,8 +810,11 @@ module ActiveRecord records_to_save.each { |record| association.send(:insert_record, record) } association.send(:construct_sql) # reconstruct the SQL queries now that we know the owner's id end + + @new_record_before_save = false + true end_eval - + # Doesn't use after_save as that would save associations added in after_create/after_update twice after_create(after_callback) after_update(after_callback) diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index ef2872e13c..df53118040 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -1397,5 +1397,14 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase assert developer.special_projects.include?(special_project) assert !developer.special_projects.include?(other_project) end - + + def test_update_attributes_after_push_without_duplicate_join_table_rows + developer = Developer.new("name" => "Kano") + project = SpecialProject.create("name" => "Special Project") + assert developer.save + developer.projects << project + developer.update_attribute("name", "Bruza") + assert_equal "1", developer.connection.select_one("SELECT count(*) FROM developers_projects WHERE + project_id = #{project.id} AND developer_id = #{developer.id}")["count(*)"] + end end -- cgit v1.2.3