Skip to content

Instantly share code, notes, and snippets.

@matiasmoya
Created June 24, 2015 17:50
Show Gist options
  • Save matiasmoya/7d059c6c4118b42e1464 to your computer and use it in GitHub Desktop.
Save matiasmoya/7d059c6c4118b42e1464 to your computer and use it in GitHub Desktop.
SQL Magic
Product
has_many reviews
...
positives = Product.includes(:reviews).select("COUNT(reviews.rating) AS cta").having("cta > 3")
...
def self.positves
includes(:reviews).select("id", "COUNT(reviews.rating) AS cta").having("cta > 3")
end
...
Product.where(:id.nin => positives.pluck(:id))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment