Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How can I programmatically find these duplicate records?
- ResponseSets.all.each do |set|
- answer_ids = []
- set.responses.each do |r|
- puts "Response #{r.id}: set=#{set.id}, answer=#{r.answer_id}"
- if answer_ids.include? r.answer_id
- puts "Duplicate found!"
- # r.destroy # Uncomment when you feel it is safe
- else
- answer_ids << r.answer_id
- end
- end
- end
- select rs.id, r.answer_id, count(r.id)
- from response_sets rs
- join responses r on r.response_set_id = rs.id
- group by rs.id, r.answer_id
- having count(r.id) > 1;
- select r.id
- from responses r
- join (
- select rs.id as rs_id, r.answer_id as a_id, r.id as r_id
- from response_sets rs
- join responses r on r.response_set_id = rs.id
- group by rs.id, r.answer_id
- having count(r.id) > 1
- ) on r.response_set_id = rs_id
- where r.answer_id = a_id and r.id != r_id;
Advertisement
Add Comment
Please, Sign In to add comment