Skip to content

Commit

Permalink
Don't use association in batch_builder
Browse files Browse the repository at this point in the history
Previously this was building codes on the association and then relying
on the autosave behaviour to save the new records when save was called
on the Promotion.

It's simpler to just explicitly create them.
  • Loading branch information
jhawthorn committed Feb 15, 2018
1 parent c38eab6 commit fa8f3f1
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions core/app/models/spree/promotion_code/batch_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,13 @@ def generate_random_codes
codes_for_current_batch += get_unique_codes(new_codes)
end

codes_for_current_batch.map do |value|
promotion.codes.build(value: value, promotion_code_batch: promotion_code_batch)
codes_for_current_batch.each do |value|
Spree::PromotionCode.create!(
value: value,
promotion: promotion,
promotion_code_batch: promotion_code_batch
)
end

promotion.save!

# We have to reload the promotion because otherwise all promotion codes
# we are creating will remain in memory. Doing a reload will remove all
# codes from memory.
promotion.reload
end
end

Expand Down

0 comments on commit fa8f3f1

Please sign in to comment.