Created
March 27, 2018 09:55
-
-
Save SeyZ/1054f38aad40376db7b8946a1a47484f to your computer and use it in GitHub Desktop.
doc-rails-smart-segment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /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