News for hackers. Stuff that matters.
Re:Understanding N + 1 queries problem by ydnaclementine on Monday January 02, 2023 @03:10PM (#34222997)
Would this not be solved with adding `votes` to the `includes`? Something like:

```

Post.includes(comments: :votes)

```

Similar stackoverflow: https://stackoverflow.com/a/24397716

Re:Understanding N + 1 queries problem by ramchip on Monday January 02, 2023 @03:16PM (#34223067)
Exactly this. Combine with Bullet[1] to detect problems early.

[1] https://bhserna.com/tools-to-help-you-detect-n-1-queries.htm...

Re:Understanding N + 1 queries problem by rurabe on Monday January 02, 2023 @04:44PM (#34224094)
The problem here is that you are loading all the votes as AR instances which is fine at small scale, but as your app gets larger, loading and instantiating thousands of Vote instances just to then break them down into an integer will start to drag on your controller.

If you can count in the database itself it's a big win. Although no doubt your solution is cleaner code.