From cf448036b65e00034ceeae439904eba45700a496 Mon Sep 17 00:00:00 2001 From: Florian Weber Date: Wed, 21 Dec 2005 16:56:32 +0000 Subject: [PATCH] Fixed that saving a model with multiple habtm associations would only save the first one. git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/stable@3332 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 6 +++++- activerecord/lib/active_record/associations.rb | 7 ++----- activerecord/test/associations_test.rb | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 2b00dd9126543..c6441a0ad79c2 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,4 +1,8 @@ -* +*SVN* + +* Fixed that saving a model with multiple habtm associations would only save the first one. #3244 [yanowitz-rubyonrails@quantumfoam.org, Florian Weber] + + *1.13.2* (December 13th, 2005) * Become part of Rails 1.0 diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index d8abe82203bd2..50b087909c8ae 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -783,7 +783,6 @@ def require_association_class(class_name) def add_multiple_associated_save_callbacks(association_name) method_name = "validate_associated_records_for_#{association_name}".to_sym define_method(method_name) do - @new_record_before_save = new_record? association = instance_variable_get("@#{association_name}") if association.respond_to?(:loaded?) if new_record? @@ -797,7 +796,8 @@ def add_multiple_associated_save_callbacks(association_name) end validate method_name - + before_save("@new_record_before_save = new_record?; true") + after_callback = <<-end_eval association = instance_variable_get("@#{association_name}") if association.respond_to?(:loaded?) @@ -809,9 +809,6 @@ def add_multiple_associated_save_callbacks(association_name) 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 diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index 610a69547707a..9d9c255746064 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -1173,6 +1173,22 @@ def test_habtm_adding_before_save_with_join_attributes end end + def test_habtm_saving_multiple_relationships + new_project = Project.new("name" => "Grimetime") + amount_of_developers = 4 + developers = (0..amount_of_developers).collect {|i| Developer.create(:name => "JME #{i}") } + + new_project.developer_ids = [developers[0].id, developers[1].id] + new_project.developers_with_callback_ids = [developers[2].id, developers[3].id] + assert new_project.save + + new_project.reload + assert_equal amount_of_developers, new_project.developers.size + amount_of_developers.times do |i| + assert_equal developers[i].name, new_project.developers[i].name + end + end + def test_build devel = Developer.find(1) proj = devel.projects.build("name" => "Projekt")