Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load messages with `includes(:user)` in RoomsController#show to avoid N+1 #3

Closed
antico5 opened this Issue Apr 11, 2019 · 2 comments

Comments

Projects
None yet
2 participants
@antico5
Copy link
Contributor

antico5 commented Apr 11, 2019

Thanks for the tutorial, it's well explained and comprehensive. One detail I would like to point out is that when you want to include the user's gravatar url in RoomMessage#as_json, you will have a N+1 query problem on your RoomsController#show action. I think it would be nice to point this out and add something like @room_messages = @room.room_messages.includes(:user) to your show action, and explain the reason for this. N+1 are a very expensive mistake in the long run and is very frequent among junior developers. You can never remind them too much about them!

@antico5 antico5 changed the title Load messages with `includes(:user)` in RoomsController#show Load messages with `includes(:user)` in RoomsController#show to avoid N+1 Apr 11, 2019

@iridakos

This comment has been minimized.

Copy link
Owner

iridakos commented Apr 11, 2019

You are absolutely right, thanks for pointing out!

I will keep this issue open until I update the article. 👍

@iridakos iridakos closed this in e3581bf Apr 11, 2019

@iridakos

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.