diff options
author | Florian Weber <csshsh@gmail.com> | 2005-12-21 15:50:31 +0000 |
---|---|---|
committer | Florian Weber <csshsh@gmail.com> | 2005-12-21 15:50:31 +0000 |
commit | c6e01f5b60c4864f0e92149d1e81077519f503d5 (patch) | |
tree | 40cc1aee95728263ea5d80c940759822b8d61aa2 /activerecord | |
parent | 43f6643b8b3a6892105ba9db23bdafd4c8d49ac3 (diff) | |
download | rails-c6e01f5b60c4864f0e92149d1e81077519f503d5.tar.gz rails-c6e01f5b60c4864f0e92149d1e81077519f503d5.tar.bz2 rails-c6e01f5b60c4864f0e92149d1e81077519f503d5.zip |
Fixed that saving a model with multiple habtm associations, would only save the first habtm association.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3331 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rwxr-xr-x | activerecord/lib/active_record/associations.rb | 5 | ||||
-rwxr-xr-x | activerecord/test/associations_test.rb | 16 |
3 files changed, 19 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index e2de2f8762..9b60de89ff 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed that saving a model with multiple habtm associations, would only save the first habtm association. #3244 [yanowitz-rubyonrails@quantumfoam.org, Florian Weber] + * Fix change_column to work with PostgreSQL 7.x and 8.x. #3141 [wejn@box.cz, Rick Olson, Scott Barron] * removed :piggyback in favor of just allowing :select on :through associations. [Tobias Luetke] diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 562a2dc6b7..ea442021c5 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -727,7 +727,6 @@ module ActiveRecord 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? @@ -741,6 +740,7 @@ module ActiveRecord end validate method_name + before_save("@new_record_before_save = new_record?; true") after_callback = <<-end_eval association = instance_variable_get("@#{association_name}") @@ -754,9 +754,6 @@ 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 diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index e088432da8..e60035a5ac 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -1194,6 +1194,22 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase 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") |