Skip to content

Instantly share code, notes, and snippets.

@ksolvesnutra
Last active April 24, 2020 10:25
Show Gist options
  • Save ksolvesnutra/7073ce29e8446ca6edd5d45535477ade to your computer and use it in GitHub Desktop.
Save ksolvesnutra/7073ce29e8446ca6edd5d45535477ade to your computer and use it in GitHub Desktop.
[issue #493] Script to link Tracker Products with Hardy's Products and alter the active/inactive status of Tracker products as per active/inactive status of Hardy's products
den = Product.find_by(name: "Daily Essential Nutrients (360)")
tracker_den_obj = Wellness::HardyProduct.where('lower(name) = ?', 'daily essential nutrients').first
tracker_den_obj.update_attributes(name: "Daily Essential Nutrients (360)", product_id: den.id, active: den.active)
# =============================================================================================
Wellness::HardyProduct.where(product_id: nil).count
Product.count
Wellness::HardyProduct.count
Product.active.count
Wellness::HardyProduct.active.count
# =============================================================================================
product_name = Product.pluck(:name)
wellness_product_name = Wellness::HardyProduct.pluck(:name)
repeating_products = product_name.find_all { |e| product_name.count(e) > 1 }.uniq
repeating_wellness_products = wellness_product_name.find_all { |e| product_name.count(e) > 1 }.uniq
# ===========================================================================================
Wellness::HardyProduct.where('name in (?)', repeating_products).find_each do |a|
puts "#{a.id}, #{a.active}, #{a.user_hardy_products.count}"
end
# ===========================================================================================
repeating_products.each do |prod_name|
product = Product.where(name: prod_name)
primary_product = product.where(active: true).present? ? product.where(active: true).first : product.where(active: false).first
secondary_product = product.where(active: true).present? ? product.where(active: false).first : product.where(active: false).second
duplicate_hardy_products = Wellness::HardyProduct.where(name: prod_name)
if duplicate_hardy_products.present?
primary_hp = duplicate_hardy_products.first
secondary_hp = duplicate_hardy_products.second
if (primary_hp.user_hardy_products.count > secondary_hp.user_hardy_products.count || primary_hp.user_hardy_products.count == secondary_hp.user_hardy_products.count)
primary_hp.update_attributes(product_id: primary_product.id, active: primary_product.active)
puts "updated hp #{primary_hp.id}"
secondary_hp.user_hardy_products.update_all(hardy_product_id: primary_hp.id)
secondary_hp.update_attributes(product_id: secondary_product.id, active: secondary_product.active)
puts "updated hp #{secondary_hp.id}"
elsif (primary_hp.user_hardy_products.count < secondary_hp.user_hardy_products.count || primary_hp.user_hardy_products.count == secondary_hp.user_hardy_products.count)
secondary_hp.update_attributes(product_id: primary_product.id, active: primary_product.active)
puts "updated hp #{secondary_hp.id}"
primary_hp.user_hardy_products.update_all(hardy_product_id: secondary_hp.id)
primary_hp.update_attributes(product_id: secondary_product.id, active: secondary_product.active)
puts "updated hp #{primary_hp.id}"
end
end
end
# ========================================================================================
Wellness::HardyProduct.where('name in (?)', repeating_products).find_each do |a|
puts "#{a.id}, #{a.active}, #{a.user_hardy_products.count}"
end
# ==========================================================================================
Product.find_each do |product|
whp_obj = Wellness::HardyProduct.where(name: product.name).first
if whp_obj.present?
if whp_obj.product_id.blank?
whp_obj.update_attributes(product_id: product.id, active: product.active)
puts "Updated hp obj: #{whp_obj.id}"
end
else
whp_obj = Wellness::HardyProduct.create(name: product.name, active: product.active, product_id: product.id)
puts "Created hp obj: #{whp_obj.id}"
end
end
# ===========================================================================================
Wellness::HardyProduct.where(product_id: nil).count
Product.count
Wellness::HardyProduct.count
Product.active.count
Wellness::HardyProduct.active.count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment