4 ms·
Would this not be solved with adding `votes` to the `includes`? Something like: ``` Post.includes(comments: :votes) ``` Similar stackoverflow: https://stack
by ydnaclementine 4y ago
Would this not be solved with adding `votes` to the `includes`? Something like:
```
Post.includes(comments: :votes)
```
Similar stackoverflow: https://stackoverflow.com/a/24397716 https://stackoverflow.com/a/24397716
- ramchip 4y agoExactly this. Combine with Bullet[1] to detect problems early. [1] https://bhserna.com/tools-to-help-you-detect-n-1-queries.html#bullet https://bhserna.com/tools-to-help-you-detect-n-1-queries.htm...
- rurabe 4y agoThe 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.