Skip to content

Instantly share code, notes, and snippets.

@danigb
Last active August 26, 2019 13:55
Show Gist options
  • Save danigb/288ec9ccf79378428da4635765e5a022 to your computer and use it in GitHub Desktop.
Save danigb/288ec9ccf79378428da4635765e5a022 to your computer and use it in GitHub Desktop.
find duplicates
Template.select([:name]).group(:name).having("count(name) > 1").reorder(nil).all.size
D, [2019-08-26T13:40:29.299683 #99] DEBUG -- :   Template Load (1.5ms)  SELECT  "templates"."name" FROM "templates" GROUP BY "templates"."name" HAVING (count(name) > 1) LIMIT $1  [["LIMIT", 11]]
=> #<ActiveRecord::Relation [#<Template id: nil, name: "Store Page (2510)">, #<Template id: nil, name: "business-jsonld">, #<Template id: nil, name: "Change me">, #<Template id: nil, name: "Hero Store Page (2510)">]>
irb(main):012:0> Template.select([:name]).group(:name).having("count(name) > 1").reorder(nil).all.count
D, [2019-08-26T13:40:34.657327 #99] DEBUG -- :    (1.6ms)  SELECT COUNT("templates"."name") AS count_name, "templates"."name", "templates"."name" AS templates_name FROM "templates" GROUP BY "templates"."name" HAVING (count(name) > 1)
=> {"Store Page (2510)"=>2, "business-jsonld"=>2, "Change me"=>2, "Hero Store Page (2510)"=>2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment