How to render users pagination when an action is performed on a certain user

158 Views Asked by At

We render users in alphabetical fashion with 10 entries per page using will_paginate gem

Assuming we perform an action to activate or deactivate a user in the 2nd page (say user15) , We would prefer to show user information on the 2nd page itself or should we redirect the user to the first page with default sort criteria here

There is another case where this is becoming problematic:

Assuming there are 31 entries, so 4th page will contain a single entry , If we delete this user, we can't show the fourth page since no such user exists In the above scenario, page=4 and per_page=10, but it would fail to render this page

How should we handle these scenarios, should we just show page=1 in any of the above scenarios.

What would be the right thing to do in the above scenario

1

There are 1 best solutions below

0
Christian Bruckmayer On

We would prefer to show user information on the 2nd page itself or should we redirect the user to the first page with default sort criteria here

Can you elaborate on this one please.

Assuming there are 31 entries, so 4th page will contain a single entry , If we delete this user, we can't show the fourth page since no such user exists In the above scenario, page=4 and per_page=10, but it would fail to render this page

You might want to look into cursor pagination. Instead of pages, you have a cursor for the record and fetch everything before / after this cursor. This cursor could for instance be the encoded created_at timestamp.

Cursor pagination has a lot of other advantages over offset pagination too. However, will_paginate does not support cursor pagination so not sure if this is a feasible approach for you.

To get around these performance concerns, some database engines offer cursor-based pagination, or encourage pagination based on min/max values, e.g. WHERE id > :max_id in which the :max_id value is based on the previous page of results. This approach is usually superior for speed and memory concerns, but comes with some tradeoffs; the most notable being that it's only ever possible to go to the next page of results and not immediately jump to page 20, for example. The will_paginate library does not handle cursor-based pagination.

https://github.com/mislav/will_paginate/wiki

https://medium.com/@meganchang_96378/why-facebook-says-cursor-pagination-is-the-greatest-d6b98d86b6c0