Skip to content

Instantly share code, notes, and snippets.

@pama
Last active January 9, 2017 18:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pama/321d09ae6b6fda349eb848114fd796d0 to your computer and use it in GitHub Desktop.
Save pama/321d09ae6b6fda349eb848114fd796d0 to your computer and use it in GitHub Desktop.
Check box filters
scope :category_query, ->(query) do
if query.count == 1
joins(:article_categories).where('article_categories.category_id': query.first)
else
joins(:article_categories).
where('article_categories.article_id': ArticleCategory.select('article_categories.article_id')
.where(category_id: query)
.group('article_categories.article_id')
.having("COUNT(article_categories.article_id) > #{query.count - 1}"))
.uniq
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment