Skip to content

Instantly share code, notes, and snippets.

@rhearnorth
Last active August 29, 2015 14:10
Show Gist options
  • Save rhearnorth/169c0ae907794821625b to your computer and use it in GitHub Desktop.
Save rhearnorth/169c0ae907794821625b to your computer and use it in GitHub Desktop.
Script for merge product with the same name. (Move variants and delete other products)
module MergeProducts
# Example
# => MergeProducts.merge_into(Product.first)
def self.merge_into(product)
fail "This is 'achived' product" if product.status.eql?('archived')
"Starting merge products ... "
# Search all product with exact same name
other_products = product.account.products.where(name: product.name).where.not(id: product.id, status: 'archived')
other_variants = other_products.collect(&:variants).flatten
# Move variants to the target product
other_variants.each do |variant|
variant.update_attribute(:product_id, product.id)
end
# Delete other products
other_products.map(&:soft_delete)
puts "Fininshed"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment