Fetching the top n per group with window functions
25-Aug-2022 732
Have you ever needed to get the most recent N posts for each user in rails, but didn’t know how to do it without using map?Or maybe something similar like:The first or last X comments for each postThe first or last Y payments for each customerThe first or last Z reviews for each customerSometimes could be ok to just fetch all elements and filter with ruby, but sometimes it is not possible. Also it can cause n 1 queries if your are not careful.Here I want to show you how you can solve this problem using window functions.
Fetching the top n per group with window functions #ruby #rubydeveloper #rubyonrails #Fetching #group #window #functions https://rubyonrails.ba/link/fetching-the-top-n-per-group-with-window-functions