Skip to content

Instantly share code, notes, and snippets.

@amitpatelx
Last active August 29, 2015 14:23
Show Gist options
  • Save amitpatelx/2b4885144c63359c2e06 to your computer and use it in GitHub Desktop.
Save amitpatelx/2b4885144c63359c2e06 to your computer and use it in GitHub Desktop.
Remove Duplicate Active Record Objects
pins = Pin.group(:name).having('count("name") > 1').count(:name)
{"Adam"=>2,
"Beulings"=>2,
"Bord'Eau (Hotel De L'Europe Amsterdam)"=>2,
"Ciel Bleu (Hotel Okura Amsterdam)"=>2,
"Coffee Plaza"=>2,
"Da Portare Via"=>3,
"Dappertutto"=>2,
"En Pluche"=>2,
"Greenwoods"=>2,
"Kagetsu"=>3,
"Koh-I-Noor"=>2,
"La Teatina"=>2,
"Le Restaurant"=>2,
"Nam Kee"=>3,
"No 1916"=>2,
"Pomorosso"=>2,
"Restaurant Jaspers"=>2,
"Restaurant Librije's Zusje Amsterdam"=>2,
"Restaurant Mesken"=>2,
"Restaurant Sinne"=>2,
"Soup en Zo"=>2,
"Toscana"=>2,
"Traiteur Indo Jaya"=>2,
"Vermeer (NH Hotel)"=>2,
"Vinkeles (the Dylan hotel)"=>2,
"Warung Mini"=>2,
"Wok to Walk"=>2,
"Zaza's"=>2}
[14] pry(main)> pins.each do |key, value|
[14] pry(main)* puts "#{key} = #{(Pin.where(name: key)[0..value-2]).count}"
(Pin.where(name: key)[1..value-1]).each(&:destroy)
[14] pry(main)* end
Pin Load (1.4ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Adam'
(0.1ms) begin transaction
ActsAsVotable::Vote Load (426.3ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 15], ["votable_type", "Pin"]]
SQL (108.1ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 15]]
(381.1ms) commit transaction
Pin Load (0.9ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Beulings'
(0.1ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 9], ["votable_type", "Pin"]]
SQL (0.1ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 9]]
(464.1ms) commit transaction
Pin Load (0.8ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Bord''Eau (Hotel De L''Europe Amsterdam)'
(0.1ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 2], ["votable_type", "Pin"]]
SQL (0.1ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 2]]
(163.1ms) commit transaction
Pin Load (0.9ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Ciel Bleu (Hotel Okura Amsterdam)'
(0.1ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 3], ["votable_type", "Pin"]]
SQL (0.1ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 3]]
(174.2ms) commit transaction
Pin Load (0.7ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Coffee Plaza'
(0.1ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 835], ["votable_type", "Pin"]]
SQL (90.5ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 835]]
(184.1ms) commit transaction
Pin Load (0.9ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Da Portare Via'
(0.1ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 941], ["votable_type", "Pin"]]
SQL (22.4ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 941]]
(173.4ms) commit transaction
(0.1ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 1196], ["votable_type", "Pin"]]
SQL (14.2ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 1196]]
(138.9ms) commit transaction
Pin Load (0.7ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Dappertutto'
(0.1ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 13], ["votable_type", "Pin"]]
SQL (0.1ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 13]]
(152.2ms) commit transaction
Pin Load (0.7ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'En Pluche'
(0.1ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 10], ["votable_type", "Pin"]]
SQL (0.2ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 10]]
(173.8ms) commit transaction
Pin Load (1.0ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Greenwoods'
(0.1ms) begin transaction
ActsAsVotable::Vote Load (0.2ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 865], ["votable_type", "Pin"]]
SQL (0.2ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 865]]
(151.1ms) commit transaction
Pin Load (2.2ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Kagetsu'
(0.2ms) begin transaction
ActsAsVotable::Vote Load (0.3ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 1133], ["votable_type", "Pin"]]
SQL (0.4ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 1133]]
(233.9ms) commit transaction
(0.1ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 1585], ["votable_type", "Pin"]]
SQL (23.5ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 1585]]
(174.3ms) commit transaction
Pin Load (0.8ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Koh-I-Noor'
(0.1ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 186], ["votable_type", "Pin"]]
SQL (0.2ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 186]]
(164.5ms) commit transaction
Pin Load (0.8ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'La Teatina'
(0.1ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 14], ["votable_type", "Pin"]]
SQL (0.2ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 14]]
(163.2ms) commit transaction
Pin Load (0.7ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Le Restaurant'
(0.1ms) begin transaction
ActsAsVotable::Vote Load (0.2ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 1], ["votable_type", "Pin"]]
SQL (4.4ms) DELETE FROM "votes" WHERE "votes"."id" = ? [["id", 18]]
SQL (0.2ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 1]]
(240.2ms) commit transaction
Pin Load (0.9ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Nam Kee'
(0.1ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 541], ["votable_type", "Pin"]]
SQL (24.0ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 541]]
(183.3ms) commit transaction
(0.1ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 613], ["votable_type", "Pin"]]
SQL (0.3ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 613]]
(186.0ms) commit transaction
Pin Load (0.8ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'No 1916'
(0.0ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 7], ["votable_type", "Pin"]]
SQL (0.1ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 7]]
(163.4ms) commit transaction
Pin Load (0.8ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Pomorosso'
(0.0ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 212], ["votable_type", "Pin"]]
SQL (0.1ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 212]]
(174.4ms) commit transaction
Pin Load (0.7ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Restaurant Jaspers'
(0.0ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 8], ["votable_type", "Pin"]]
SQL (0.2ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 8]]
(163.3ms) commit transaction
Pin Load (0.8ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Restaurant Librije''s Zusje Amsterdam'
(0.0ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 11], ["votable_type", "Pin"]]
SQL (0.2ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 11]]
(163.3ms) commit transaction
Pin Load (0.7ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Restaurant Mesken'
(0.0ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 1101], ["votable_type", "Pin"]]
SQL (0.2ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 1101]]
(152.2ms) commit transaction
Pin Load (0.8ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Restaurant Sinne'
(0.0ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 5], ["votable_type", "Pin"]]
SQL (0.1ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 5]]
(152.4ms) commit transaction
Pin Load (0.7ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Soup en Zo'
(0.0ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 683], ["votable_type", "Pin"]]
SQL (0.2ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 683]]
(185.5ms) commit transaction
Pin Load (1.4ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Toscana'
(0.1ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 1449], ["votable_type", "Pin"]]
SQL (19.3ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 1449]]
(153.0ms) commit transaction
Pin Load (0.7ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Traiteur Indo Jaya'
(0.0ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 685], ["votable_type", "Pin"]]
SQL (0.2ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 685]]
(230.0ms) commit transaction
Pin Load (0.7ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Vermeer (NH Hotel)'
(0.0ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 6], ["votable_type", "Pin"]]
SQL (0.1ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 6]]
(218.8ms) commit transaction
Pin Load (0.8ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Vinkeles (the Dylan hotel)'
(0.0ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 4], ["votable_type", "Pin"]]
SQL (0.1ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 4]]
(208.9ms) commit transaction
Pin Load (0.7ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Warung Mini'
(0.1ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 1440], ["votable_type", "Pin"]]
SQL (0.1ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 1440]]
(141.0ms) commit transaction
Pin Load (0.7ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Wok to Walk'
(0.1ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 1293], ["votable_type", "Pin"]]
SQL (0.2ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 1293]]
(141.2ms) commit transaction
Pin Load (0.7ms) SELECT "pins".* FROM "pins" WHERE "pins"."name" = 'Zaza''s'
(0.0ms) begin transaction
ActsAsVotable::Vote Load (0.1ms) SELECT "votes".* FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? [["votable_id", 12], ["votable_type", "Pin"]]
SQL (0.2ms) DELETE FROM "pins" WHERE "pins"."id" = ? [["id", 12]]
(219.4ms) commit transaction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment