Skip to content

Instantly share code, notes, and snippets.

@antico5
Created February 27, 2018 14:58
Show Gist options
  • Save antico5/098518fc9879a5f4aabb034997f9cc1d to your computer and use it in GitHub Desktop.
Save antico5/098518fc9879a5f4aabb034997f9cc1d to your computer and use it in GitHub Desktop.
Select count having
User.select('sales_person_id, COUNT(1) as us_count').group(:sales_person_id).having('COUNT(1) > 1')
class ApplicationRecord
def self.group_having(field, count)
select("#{field}, COUNT(1) as count").group(field).having("COUNT(1) > #{count}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment