aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/nested_attributes_test.rb
diff options
context:
space:
mode:
authorJames Le Cuirot <chewi@aura-online.co.uk>2010-06-12 11:55:31 +0100
committerJosé Valim <jose.valim@gmail.com>2010-06-20 00:32:06 +0200
commitf664c57fe854c230d25c448b941569b81860fa79 (patch)
treeabec332dc7b0981c9ee43090b7aacbe1827fbdf1 /activerecord/test/cases/nested_attributes_test.rb
parent98a5188f518b1e8cfa9d7aa59856e5b32985eb46 (diff)
downloadrails-f664c57fe854c230d25c448b941569b81860fa79.tar.gz
rails-f664c57fe854c230d25c448b941569b81860fa79.tar.bz2
rails-f664c57fe854c230d25c448b941569b81860fa79.zip
Don't overwrite unsaved updates when loading an association but preserve the order of the loaded records. Reapplied from before but now allows already-saved records to be refreshed.
[#4830 state:resolved] Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'activerecord/test/cases/nested_attributes_test.rb')
-rw-r--r--activerecord/test/cases/nested_attributes_test.rb21
1 files changed, 21 insertions, 0 deletions
diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb
index 685b11cb03..65d6080ea5 100644
--- a/activerecord/test/cases/nested_attributes_test.rb
+++ b/activerecord/test/cases/nested_attributes_test.rb
@@ -466,6 +466,27 @@ module NestedAttributesOnACollectionAssociationTests
assert_equal 'Grace OMalley', @child_1.reload.name
end
+ def test_should_not_overwrite_unsaved_updates_when_loading_association
+ @pirate.reload
+ @pirate.send(association_setter, [{ :id => @child_1.id, :name => 'Grace OMalley' }])
+ assert_equal 'Grace OMalley', @pirate.send(@association_name).send(:load_target).find { |r| r.id == @child_1.id }.name
+ end
+
+ def test_should_preserve_order_when_not_overwriting_unsaved_updates
+ @pirate.reload
+ @pirate.send(association_setter, [{ :id => @child_1.id, :name => 'Grace OMalley' }])
+ assert_equal @child_1.id, @pirate.send(@association_name).send(:load_target).first.id
+ end
+
+ def test_should_refresh_saved_records_when_not_overwriting_unsaved_updates
+ @pirate.reload
+ record = @pirate.class.reflect_on_association(@association_name).klass.new(:name => 'Grace OMalley')
+ @pirate.send(@association_name) << record
+ record.save!
+ @pirate.send(@association_name).last.update_attributes!(:name => 'Polly')
+ assert_equal 'Polly', @pirate.send(@association_name).send(:load_target).last.name
+ end
+
def test_should_take_a_hash_with_composite_id_keys_and_assign_the_attributes_to_the_associated_models
@child_1.stubs(:id).returns('ABC1X')
@child_2.stubs(:id).returns('ABC2X')