Skip to content

Instantly share code, notes, and snippets.

@SeyZ
Created March 27, 2018 09:55
Show Gist options
  • Save SeyZ/1054f38aad40376db7b8946a1a47484f to your computer and use it in GitHub Desktop.
Save SeyZ/1054f38aad40376db7b8946a1a47484f to your computer and use it in GitHub Desktop.
doc-rails-smart-segment
# /lib/forest_liana/collections/product.rb
class Forest::Product
include ForestLiana::Collection
collection :Product
segment 'Bestsellers' do
products = ActiveRecord::Base.connection.execute("""
SELECT products.id, COUNT(orders.*)
FROM products
JOIN orders ON orders.product_id = products.id
GROUP BY products.id
ORDER BY count DESC
LIMIT 10;
""").to_a
{ id: products.map { |p| p['id'] } }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment