Rails Fix N+1 Query
How to fix N+1 query in Rails ?
I try to explain fix N+1
on example. Let’s start, firstly, in your rails application, you created association between user
and user_profile
models like below :
1 2 3 |
|
and
1 2 3 |
|
When you created association between user
and user_profile
like above (you can derive it), then you are getting like below error while you refresh to app page or
edit something… The error is that :
1 2 3 4 5 6 |
|
Actually rails says to you for fix above bug(it says use include
to profile
…), for fix bug open users_controller.rb
then
add :
1
|
|
instead of
1
|
|
like below ;
1 2 3 4 5 6 |
|
That’s it.
I hope helped to you .