Skip to content

Commit 6783f61

Browse files
committed
order quick order by most ordered
1 parent 4c75642 commit 6783f61

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

app/models/user.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,15 @@ def balance
108108
end
109109

110110
def last_ordered_products(amount = 5)
111-
orders.includes(:products)
111+
products = orders.includes(:products)
112112
.order(created_at: :desc)
113-
.limit(amount)
113+
.limit(amount + 15)
114114
.flat_map(&:products)
115-
.uniq
116-
.first(amount)
115+
116+
products.tally
117+
.sort_by { |_, count| -count }
118+
.map(&:first)
119+
.first(amount)
117120
end
118121

119122
# Static Users

0 commit comments

Comments
 (0)