Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@inopinatus
Created August 24, 2020 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inopinatus/6c98eab999eef4573040af40839efba2 to your computer and use it in GitHub Desktop.
Save inopinatus/6c98eab999eef4573040af40839efba2 to your computer and use it in GitHub Desktop.
AR generalised association join, group, count, and filter
# app/models/concerns/exceeding.rb
module Exceeding
extend ActiveSupport::Concern
included do
scope :exceeds, ->(association, n) do
joins(association).group(primary_key).having(arel_table[primary_key].count.gteq n)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment