Last active
August 29, 2015 14:10
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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