You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: rails6/en/chapter09-optimization.adoc
+15-14Lines changed: 15 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ Welcome to the last chapter of the book. It has been a long way, but you are onl
8
8
* optimization of SQL queries
9
9
* the activation of CORS
10
10
11
-
I will try to go as far as I can by trying to cover some common scenarios. I hope that these scenarios will be useful for some of your projects.
11
+
I will try to go as far as I can by trying to cover some common scenarios. I hope these scenarios will be useful for some of your projects.
12
12
13
13
If you start reading at this point, you'll probably want the code to work, you can clone it like that:
14
14
@@ -27,7 +27,7 @@ $ git checkout -b chapter09
27
27
28
28
== Pagination
29
29
30
-
A very common strategy to optimize an array of records from the database, is to load just a few by paginating them and if you are familiar with this technique you know that in Rails is really easy to achieve it whether if you are using https://github.com/mislav/will_paginate[will_paginate] or https://github.com/amatsuda/kaminari[kaminari].
30
+
A very common strategy to optimize an array of records from the database, is to load just a few by paginating them and if you are familiar with this technique you know that in Rails that's really easy to achieve it whether if you are using https://github.com/mislav/will_paginate[will_paginate] or https://github.com/amatsuda/kaminari[kaminari].
31
31
32
32
Then only tricky part in here is how are we suppose to handle the JSON output now, to give enough information to the client on how the array is paginated. If you recall first chapter I shared some resources on the practices I was going to be following in here. One of them was http://jsonapi.org/ which is a must-bookmark page.
33
33
@@ -87,7 +87,7 @@ We need to provide the pagination information on the `meta` tag in the following
87
87
}
88
88
----
89
89
90
-
Now that we have the final structure for the `meta` tag we just need to output it on the JSON response. Let’s first add some specs:
90
+
Now we have the final structure for the `meta` tag we just need to output it on the JSON response. Let’s first add some specs:
Now that we have made a superb optimization for the product list route, it is up to the customer to retrieve the `page` with the right `per_page` parameter for registrations.
180
+
Now we have made a superb optimization for the product list route, it is up to the customer to retrieve the `page` with the right `per_page` parameter for registrations.
181
181
182
182
Let's make these changes and continue with the list of commands.
183
183
@@ -347,7 +347,7 @@ $ rake test
347
347
----
348
348
349
349
350
-
Now that we have done this simple factorization for testing, we can move on to the implementation of pagination for controllers and clean things up. If you remember the indexing action for both product and order controllers, they both have the same pagination format. So let's move this logic into a method called `get_links_serializer_options` under the file `paginable.rb`, so we can access it on any controller that would need paging.
350
+
Now we have done this simple factorization for testing, we can move on to the implementation of pagination for controllers and clean things up. If you remember the indexing action for both product and order controllers, they both have the same pagination format. So let's move this logic into a method called `get_links_serializer_options` under the file `paginable.rb`, so we can access it on any controller that would need paging.
351
351
352
352
353
353
[source,ruby]
@@ -492,7 +492,7 @@ $ git commit -am "Adds caching for the serializers"
492
492
493
493
N+1* requests are a wound that can have a huge impact on the performance of an application. This phenomenon often occurs when using a **ORM** because it generates **automatically** SQL queries for us. This handy tool is double-edged because it can generate a **large number** of SQL queries.
494
494
495
-
Something to know about SQL queries is that it is better to limit the number. In other words, a large request is often more efficient than a hundred small ones.
495
+
Something to know about SQL queries is that it's better limiting the number. In other words, a large request is often more efficient than a hundred small ones.
496
496
497
497
Here is an example where we want to recover all users who have already created a product. Open the Rails console with `Console Rails` and execute the following Ruby code:
498
498
@@ -527,9 +527,9 @@ User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" IN (?, ?, ?)
527
527
528
528
Rails makes a second request that will retrieve *all* users at once.
529
529
530
-
=== Prevention des requêtes N+1
530
+
=== Prevention of N + 1 requests
531
531
532
-
Imagine that we want to add the owners of the products for the road `/products`. We have already seen that with the `fast_jsonapi` library it is very easy to do this:
532
+
Imagine we want adding owners of the products for the road `/products`. We have already seen that with the `fast_jsonapi` library it is very easy to do this:
533
533
534
534
[source,ruby]
535
535
.app/controllers/api/v1/products_controller.rb
@@ -547,14 +547,14 @@ class Api::V1::ProductsController < ApplicationController
547
547
end
548
548
----
549
549
550
-
Now let's make a request with cURL. I remind you that we must obtain an authentication token before accessing the page.
550
+
Now let's make a request with cURL. I remind you we must obtain an authentication token before accessing the page.
551
551
552
552
[source,bash]
553
553
----
554
554
$ curl -X POST --data "user[email][email protected]" --data "user[password]=locadex1234" http://localhost:3000/api/v1/tokens
555
555
----
556
556
557
-
NOTE: "[email protected]" corresponds to a user created in my application with the _seed_. In your case, it will probably be different from mine since we used the Faker library.
557
+
NOTE: "[email protected]" corresponds to an user created in my application with the _seed_. In your case, it will probably be different from mine since we used the Faker library.
558
558
559
559
With the help of the token obtained, we can now make a request to access the products
560
560
@@ -624,11 +624,12 @@ USE eager loading detected
624
624
Add to your finder: :includes => [:user]
625
625
----
626
626
627
-
Il nous indique même comment la corriger :
627
+
He even tells us how to correct it:
628
628
629
-
> Ajouter à votre moteur de recherche : :includes => [:user]
629
+
> Add to your search engine:: includes => [: user]
630
+
631
+
So correct our error so the controller:
630
632
631
-
Corrigeons donc notre erreur donc le contrôleur:
632
633
633
634
[source,ruby]
634
635
.app/controllers/api/v1/products_controller.rb
@@ -650,7 +651,7 @@ class Api::V1::ProductsController < ApplicationController
650
651
end
651
652
----
652
653
653
-
Et voilà! Il est maintenant temps de faire notre _commit_.
0 commit comments