diff --git a/activerecord/lib/active_record/association_relation.rb b/activerecord/lib/active_record/association_relation.rb index 41571857b3..7bcefe71ad 100644 --- a/activerecord/lib/active_record/association_relation.rb +++ b/activerecord/lib/active_record/association_relation.rb @@ -27,16 +27,6 @@ module ActiveRecord RUBY end - def build(attributes = nil, &block) - if attributes.is_a?(Array) - attributes.collect { |attr| build(attr, &block) } - else - block = current_scope_restoring_block(&block) - scoping { _new(attributes, &block) } - end - end - alias new build - private def _new(attributes, &block) @association.build(attributes, &block) diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 4a053689d5..3bd6e476c5 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -67,8 +67,12 @@ module ActiveRecord # user = users.new { |user| user.name = 'Oscar' } # user.name # => Oscar def new(attributes = nil, &block) - block = current_scope_restoring_block(&block) - scoping { _new(attributes, &block) } + if attributes.is_a?(Array) + attributes.collect { |attr| new(attr, &block) } + else + block = current_scope_restoring_block(&block) + scoping { _new(attributes, &block) } + end end alias build new diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index c72a7cc5e2..90052a388c 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -1278,6 +1278,34 @@ class RelationTest < ActiveRecord::TestCase assert_equal post, comment.post end + def test_new_with_array + green_birds = Bird.where(color: "green").new([{ name: "parrot" }, { name: "canary" }]) + assert_equal ["parrot", "canary"], green_birds.map(&:name) + assert_equal ["green", "green"], green_birds.map(&:color) + green_birds.each { |bird| assert_not_predicate bird, :persisted? } + end + + def test_build_with_array + green_birds = Bird.where(color: "green").build([{ name: "parrot" }, { name: "canary" }]) + assert_equal ["parrot", "canary"], green_birds.map(&:name) + assert_equal ["green", "green"], green_birds.map(&:color) + green_birds.each { |bird| assert_not_predicate bird, :persisted? } + end + + def test_create_with_array + green_birds = Bird.where(color: "green").create([{ name: "parrot" }, { name: "canary" }]) + assert_equal ["parrot", "canary"], green_birds.map(&:name) + assert_equal ["green", "green"], green_birds.map(&:color) + green_birds.each { |bird| assert_predicate bird, :persisted? } + end + + def test_create_bang_with_array + green_birds = Bird.where(color: "green").create!([{ name: "parrot" }, { name: "canary" }]) + assert_equal ["parrot", "canary"], green_birds.map(&:name) + assert_equal ["green", "green"], green_birds.map(&:color) + green_birds.each { |bird| assert_predicate bird, :persisted? } + end + def test_first_or_create parrot = Bird.where(color: "green").first_or_create(name: "parrot") assert_kind_of Bird, parrot