Skip to content

Instantly share code, notes, and snippets.

@kalupa
Created February 22, 2013 17:35
Show Gist options
  • Save kalupa/5015184 to your computer and use it in GitHub Desktop.
Save kalupa/5015184 to your computer and use it in GitHub Desktop.
def update_cousin_variants_by_variant_value(variant_value)
first_sibling_value = variant_value.category.variant_values.first
existing_variants_with_this_value = self.variants.includes(:variant_values).where('variant_value_id = ?', first_sibling_value.id)
new_variants = []
existing_variants_with_this_value.each do |variant|
new_variant = product.variant.create(price: self.price)
variant.variant_values.each do |old_variant_value|
if old_variant_value.variant_category != variant_value.variant_category
new_variant.variant_values << old_variant_value
else
new_variant.variant_values << variant_value
end
end
new_variants << new_variant
end
new_variants
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment