Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Would this not be solved with adding `votes` to the `includes`? Something like:

```

Post.includes(comments: :votes)

```

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



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.


Exactly this. Combine with Bullet[1] to detect problems early.

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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: