Prevention of N + 1 Requests
Learn how to prevent the application from generating N+1 queries.
We'll cover the following...
Include user
Imagine that we want to add the owners of the products for the path /products
. We have already seen that it is straightforward to do this with the fast_jsonapi
library. We will modify the code in app/controllers/api/v1/products_controller.rb
:
ruby-2.6.3
Now, let’s request with cURL
. Remember that we must obtain an authentication token before accessing the page. Click on the “Run” button to open a terminal. Then open another terminal by clicking the “” button next to the “Terminal” tab. Initialize the rails server on the new terminal using this command:
$ cd usercode/workspace/market_place_api && rails s
Switch back to the first terminal and run:
$ curl -X POST --data "user[email]=jc.bailey@vonrueden.co" --data "user[password]=locadex1234" http://127.0.0.1:3000/api/v1/tokens
Note:
jc.bailey@vonrueden.co
corresponds to a user created in the application with the seed.
We can now request to access ...