aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/nested_attributes_test.rb
diff options
context:
space:
mode:
authorDarwin D Wu <wuddarwin@gmail.com>2018-08-15 17:18:51 -0700
committerDarwin D Wu <wuddarwin@gmail.com>2018-09-11 00:13:09 -0700
commit5291044a1d7866d2942276d812a5d3a72a67ef27 (patch)
tree3bb6f91fdec76505a2ff1104adda74dd6773dd99 /activerecord/test/cases/nested_attributes_test.rb
parent5b0b1ee8fda1cd086653992f812f96c62fb3c24b (diff)
downloadrails-5291044a1d7866d2942276d812a5d3a72a67ef27.tar.gz
rails-5291044a1d7866d2942276d812a5d3a72a67ef27.tar.bz2
rails-5291044a1d7866d2942276d812a5d3a72a67ef27.zip
Fixes #33610
In order to avoid double assignments of nested_attributes for `has_many` relations during record initialization, nested_attributes in `create_with` should not be passed into `klass.new` and have them populate during `initialize_internals_callback` with scope attributes. However, `create_with` keys should always have precedence over where clauses, so if there are same keys in both `create_with` and `where_values_hash`, the value in `create_with` should be the one that's used.
Diffstat (limited to 'activerecord/test/cases/nested_attributes_test.rb')
-rw-r--r--activerecord/test/cases/nested_attributes_test.rb12
1 files changed, 12 insertions, 0 deletions
diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb
index aa519fd332..bb1c1ea17d 100644
--- a/activerecord/test/cases/nested_attributes_test.rb
+++ b/activerecord/test/cases/nested_attributes_test.rb
@@ -217,6 +217,18 @@ class TestNestedAttributesInGeneral < ActiveRecord::TestCase
mean_pirate.parrot_attributes = { name: "James" }
assert_equal "James", mean_pirate.parrot.name
end
+
+ def test_should_not_create_duplicates_with_create_with
+ Man.accepts_nested_attributes_for(:interests)
+
+ assert_difference("Interest.count", 1) do
+ Man.create_with(
+ interests_attributes: [{ topic: "Pirate king" }]
+ ).find_or_create_by!(
+ name: "Monkey D. Luffy"
+ )
+ end
+ end
end
class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase