190 Pagination [0.5d]

Updated . Posted . Visible to the public.

Rendering thousands of records on one page is slow. Pagination loads one page at a time and keeps large lists fast.

Important

Work on this lesson in advisor mode.

Learning goals

  • You can explain why rendering a large collection on one page is slow — queries, object instantiation, rendering — and how pagination avoids it.
  • You can add pagination to a list with a gem (e.g. will_paginate) and style its controls to match the app.
  • You can read the queries pagination produces (a LIMIT/OFFSET query and a count) and explain what each is for.
  • You can create large amounts of test data quickly, e.g. inside a single transaction.

Resources

Read what's new to you, skim what's familiar, skip what you already master. Stop when you can meet the learning goals.

Your agent can also generate an overview, a tutorial or an explanation for anything here, tailored to what you already know. Just ask.

Exercises

  • Create 7500 movies in MovieDB (hint: Doing it in a single transaction is much faster). Load the movies index and measure how long it renders.
  • Use the will_paginate gem to add pagination to MovieDB's list of movies. How long does the movies index render now?
  • Inspect the HTML generated by will_paginate. Customize the style so it matches the look of your MovieDB.
  • tail -f log/development.log and see which queries will_paginate generates. What do they do?
Profile picture of Henning Koch
Henning Koch
Last edit
Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra Curriculum (2015-07-08 17:29)