Question

Im trying to find a way to delete all duplicated entries in a table of my db. Cause I have to make some further calculations with this duplicates before I delete them, I will have to put them in a temporary table.

Retrieving of the duplicates is a rather complex sql statement, which I rather don't want do execute in active_record manner...

SELECT bad_rows. *
FROM settings AS bad_rows
INNER JOIN (

    SELECT user_id, record_id, MIN( id ) AS min_id
    FROM settings
    GROUP BY user_id, record_id
    HAVING COUNT( * ) >1

) AS good_rows 
ON good_rows.user_id = bad_rows.user_id
AND good_rows.record_id = bad_rows.record_id
AND good_rows.min_id <> bad_rows.id

How can I generate this temporary table with this statement? Does it make sense to use ar-extentions for this task?

Was it helpful?

Solution

I found this link: http://www.ruby-forum.com/topic/55322

Where someone else had a similar problem. They basically recommended going straight through AR's execute() method, or using migration style code (e.g create_table :my_table etc).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top