Fixing a bug in my lateral joins queries with rails

I have some posts about using lateral joins in rails to fetch the &ldquo;top N&rdquo; of each record. Some months ago Ben Sheldon helped me see a performance problem on the queries that I was using. Here I try to explain the problem, the solution proposed by him, and also an introduction about a tool he published to build this kind of &ldquo;top N&rdquo; queries using lateral joins . The problem…