Skip to content

Instantly share code, notes, and snippets.

@sclinede
Last active August 29, 2015 14:14
Show Gist options
  • Save sclinede/74fb36cbc15b483b286e to your computer and use it in GitHub Desktop.
Save sclinede/74fb36cbc15b483b286e to your computer and use it in GitHub Desktop.
company_id = 13011776
real_regions = Company.find(company_id).region_ids_for_binding.join(',')
ActiveRecord::Base.connection.execute <<-SQL
UPDATE product_regions prs SET is_deleted = true, is_public = false
FROM (
SELECT pr.id FROM products p JOIN product_regions pr ON p.id = pr.product_id
AND p.company_id = #{company_id}
AND pr.is_deleted IS false
WHERE pr.region_id NOT IN (#{real_regions})) t WHERE t.id = prs.id;
SQL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment