Skip to content

Instantly share code, notes, and snippets.

@lbergen
Created June 21, 2012 05:11
Show Gist options
  • Save lbergen/2963993 to your computer and use it in GitHub Desktop.
Save lbergen/2963993 to your computer and use it in GitHub Desktop.
def non_multi_dupes
non_multi_specs = Spec.find_all_by_is_multival(false).select{|x| parent = x.try(:parent); !parent.try(:is_multival)}
non_multi_sids = non_multi_specs.collect {|s| s.id}
# this sql will find all spec values grouped by product_id+spec_id where the count of this grouping is greater than 1
sql = ["select * from spec_values where spec_id in (?) group by spec_id, product_id having (count(*) > 1)", non_multi_sids]
puts "about to do a beefy sql statement. Shouldn't take longer than 10 seconds or so."
single_dupe_svs = SpecValue.find_by_sql(sql)
result = []
# since the grouping done by the sql statement has squished all duplicates,
# we need to re-find the one surviving duplicate's brothers
single_dupe_svs.each do |sv|
result << [sv.product_id, SpecValue.find_all_by_product_id_and_spec_id(sv.product_id, sv.spec_id)]
end
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment