Paginate plans index with Turbo Frame infinite scroll#96
Conversation
- Add :created_by_user to includes to fix N+1 queries on the plans index - Switch unread counts query from subquery to .map(&:id) on loaded records - Paginate plans at 20 per page using LIMIT/OFFSET - Implement infinite scroll via Turbo Frames with loading: :lazy - Extract plan list items into _plan_page partial - Skip filter UI queries (plan_types, onboarding) for Turbo Frame requests Amp-Thread-ID: https://ampcode.com/threads/T-019db090-8247-7319-b0b6-e4cc4ef4b28c Co-authored-by: Amp <amp@ampcode.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 950b3df52d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
|
||
| @plan_types = PlanType.order(:name) | ||
| @page = (params[:page] || 1).to_i | ||
| @plans = plans.limit(PER_PAGE + 1).offset((@page - 1) * PER_PAGE) |
There was a problem hiding this comment.
Stabilize sort key before offset pagination
Using offset pagination here without a deterministic tie-breaker means pages can skip or duplicate plans when multiple records share the same updated_at value. The query still orders only by updated_at, so frame page=2 can return overlapping or missing rows relative to page=1 for ties, which breaks infinite scroll correctness; add a secondary unique order (for example id) before applying offset.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Good catch — added id: :desc as a secondary sort key in 283dc0d to make the offset pagination deterministic.
Amp-Thread-ID: https://ampcode.com/threads/T-019db090-8247-7319-b0b6-e4cc4ef4b28c Co-authored-by: Amp <amp@ampcode.com>
Problem
The plans index loads all plans in a single request with no pagination. With ~6,500+ users and growing plan counts, this causes:
created_by_user(missing fromincludes)Changes
:created_by_usertoincludes().map(&:id)on loaded records instead of.select(:id)subqueryloading: :lazy_plan_pagepartial@plan_types, onboarding banner)Testing