提交 025187d9 编写于 作者: S Sean Griffin

Move flattening records added to an association farther out

There are many ways that things end up getting passed to `concat`. Not
all of those entry points called `flatten` on their input. It seems that
just about every method that is meant to take a single record, or that
splats its input, is meant to also take an array. `concat` is the
earliest point that is common to all of the methods which add records to
the association. Partially fixes #18689
上级 71003d63
......@@ -151,6 +151,7 @@ def create!(attributes = {}, &block)
# be chained. Since << flattens its argument list and inserts each record,
# +push+ and +concat+ behave identically.
def concat(*records)
records = records.flatten
if owner.new_record?
load_target
concat_records(records)
......@@ -549,7 +550,7 @@ def replace_common_records_in_memory(new_target, original_target)
def concat_records(records, should_raise = false)
result = true
records.flatten.each do |record|
records.each do |record|
raise_on_type_mismatch!(record)
add_to_target(record) do |rec|
result &&= insert_record(rec, true, should_raise) unless owner.new_record?
......
......@@ -180,4 +180,13 @@ class ::SpecialReply < ::Reply
SpecialTopic.reset_counters(special.id, :lightweight_special_replies)
end
end
test "counters are updated both in memory and in the database on create" do
car = Car.new(engines_count: 0)
car.engines = [Engine.new, Engine.new]
car.save!
assert_equal 2, car.engines_count
assert_equal 2, car.reload.engines_count
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册