ActiveRecord objects of the class ‘Location’ (representing the db-table Locations) have the attributes ‘url’, ‘lat’ (latitude) and ‘lng’ (longitude).
Lat-lng-combinations on this model should be unique. The problem is, that there are a lot of Location-objects in the database having duplicate lat-lng-combinations.
I need help in doing the following
- Find objects that share the same lat-lng-combination.
- If the ‘url’ attribute of the object isn’t empty, keep this object and delete the other duplicates. Otherwise just choose the oldest object (by checking the attribute ‘created_at’) and delete the other duplicates.
As this is a one-time-operation, solutions in SQL (MySQL 5.1 compatible) are welcome too.
If it’s a one time thing then I’d just do it in Ruby and not worry too much about efficiency. I haven’t tested this thoroughly, check the sorting and such to make sure it’ll do exactly what you want before running this on your db 🙂
Now like I said, this is definitely poor, poor code. But sometimes just hacking out the thing that works is worth the time saved in thinking up something ‘better’, especially if it’s just a one-off.