Skip to content

Commit c77336c

Browse files
authored
Chapter 09 corrigé Lorène
1 parent ff9482e commit c77336c

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

rails6/en/chapter09-optimization.adoc

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Welcome to the last chapter of the book. It has been a long way, but you are onl
88
* optimization of SQL queries
99
* the activation of CORS
1010
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.
1212

1313
If you start reading at this point, you'll probably want the code to work, you can clone it like that:
1414

@@ -27,7 +27,7 @@ $ git checkout -b chapter09
2727

2828
== Pagination
2929

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].
3131

3232
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.
3333

@@ -87,7 +87,7 @@ We need to provide the pagination information on the `meta` tag in the following
8787
}
8888
----
8989

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:
9191

9292
[source,ruby]
9393
.test/controllers/api/v1/products_controller_test.rb
@@ -177,7 +177,7 @@ $ rake test
177177
42 runs, 65 assertions, 0 failures, 0 errors, 0 skips
178178
----
179179

180-
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.
181181

182182
Let's make these changes and continue with the list of commands.
183183

@@ -347,7 +347,7 @@ $ rake test
347347
----
348348

349349

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.
351351

352352

353353
[source,ruby]
@@ -492,7 +492,7 @@ $ git commit -am "Adds caching for the serializers"
492492

493493
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.
494494

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.
496496

497497
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:
498498

@@ -527,9 +527,9 @@ User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" IN (?, ?, ?)
527527

528528
Rails makes a second request that will retrieve *all* users at once.
529529

530-
=== Prevention des requêtes N+1
530+
=== Prevention of N + 1 requests
531531

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:
533533

534534
[source,ruby]
535535
.app/controllers/api/v1/products_controller.rb
@@ -547,14 +547,14 @@ class Api::V1::ProductsController < ApplicationController
547547
end
548548
----
549549

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.
551551

552552
[source,bash]
553553
----
554554
$ curl -X POST --data "user[email][email protected]" --data "user[password]=locadex1234" http://localhost:3000/api/v1/tokens
555555
----
556556

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.
558558

559559
With the help of the token obtained, we can now make a request to access the products
560560

@@ -624,11 +624,12 @@ USE eager loading detected
624624
Add to your finder: :includes => [:user]
625625
----
626626

627-
Il nous indique même comment la corriger :
627+
He even tells us how to correct it:
628628

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:
630632

631-
Corrigeons donc notre erreur donc le contrôleur:
632633

633634
[source,ruby]
634635
.app/controllers/api/v1/products_controller.rb
@@ -650,7 +651,7 @@ class Api::V1::ProductsController < ApplicationController
650651
end
651652
----
652653

653-
Et voilà! Il est maintenant temps de faire notre _commit_.
654+
There you go! It is now time to do our _commit_.
654655

655656
[source,bash]
656657
----

0 commit comments

Comments
 (0)