diff --git a/app/services/sets/product_set.rb b/app/services/sets/product_set.rb index 6222a96c51..06f3ac4f4f 100644 --- a/app/services/sets/product_set.rb +++ b/app/services/sets/product_set.rb @@ -13,9 +13,10 @@ module Sets end def save - @collection_hash.each_value.all? do |product_attributes| + # Attempt to save all records, collecting model errors. + @collection_hash.each_value.map do |product_attributes| update_product_attributes(product_attributes) - end + end.all? end def collection_attributes=(attributes) diff --git a/spec/services/sets/product_set_spec.rb b/spec/services/sets/product_set_spec.rb index 9b6fd5d2f8..e68601f887 100644 --- a/spec/services/sets/product_set_spec.rb +++ b/spec/services/sets/product_set_spec.rb @@ -229,6 +229,33 @@ describe Sets::ProductSet do expect(product_set.collection[0]).to eq product_a.reload expect(product_set.collection[1]).to eq product_b.reload end + + context 'first product has an error' do + let(:collection_hash) do + { + 0 => { + id: product_a.id, + name: "", # Product Name can't be blank + }, + 1 => { + id: product_b.id, + name: "Bananes", + }, + } + end + + it 'continues to update subsequent products' do + product_set.save + + # Errors are logged on the model + first_item = product_set.collection[0] + expect(first_item.errors.full_messages.to_sentence).to eq "Product Name can't be blank" + expect(first_item.name).to eq "" + + # Subsequent product was updated + expect(product_b.reload.name).to eq "Bananes" + end + end end end end