Skip to content

Commit

Permalink
Merge pull request #4146 from jarednorman/why-would-you-ever-want-the…
Browse files Browse the repository at this point in the history
…-same-option-value-on-a-variant-twice

Make option value to variant association unique
  • Loading branch information
kennyadsl authored Apr 26, 2023
2 parents 1a0610f + 672116e commit 05dd2e2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

class AddUniqueIndexToOptionValuesVariants < ActiveRecord::Migration[5.2]
def up
remove_index :spree_option_values_variants, [:variant_id, :option_value_id]
add_index :spree_option_values_variants, [:variant_id, :option_value_id],
name: "index_option_values_variants_on_variant_id_and_option_value_id",
unique: true
end

def down
remove_index :spree_option_values_variants, [:variant_id, :option_value_id]
add_index :spree_option_values_variants, [:variant_id, :option_value_id],
name: "index_option_values_variants_on_variant_id_and_option_value_id"
end
end
5 changes: 5 additions & 0 deletions core/spec/models/spree/variant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1042,4 +1042,9 @@
subject { variant.sku_and_options_text }
it { is_expected.to eq("EB1 Size: S") }
end

it "cannot have the same option value multiple times" do
expect { variant.option_values << variant.option_values.first }
.to raise_error ActiveRecord::RecordNotUnique
end
end

0 comments on commit 05dd2e2

Please sign in to comment.