首页 > 解决方案 > Rails API 模式 JBuilder 分页

问题描述

我目前正在尝试处理一个包含超过 9000 条记录的大型数据集,并希望对其进行分页。我从未使用过分页,我知道 Kaminari 和 Pagy 可以做到这一点。我相信我需要用 Kaminari 提供的方法或 Pagy 来包装我的发现,但我不确定在那之后我应该如何处理我的 Jbuilder 视图。下面是我的 find 方法的片段和它的构建器。与会者散列大约有 9000 条记录,导致负载超时或大约需要 5 分钟。我希望分页可以解决这个问题。

Controller
  class ConferencesController < ::ApplicationController
    def attendees
      @conference = Conference.find(attendee_params[:conference_id])
    end

    private

    def attendee_params
      params.permit(:conference_id)
    end
  end
end
View
json.full_name attendee.full_name
json.email attendee.email
json.requires_certification attendee.requires_certification

输出

{
  "conference": {
    "title": "Avengers Assemble",
    "description": null,
    "starts_at": "2021-04-21T16:00:00.0000+0000",
    "ends_at": "2021-04-21T16:45:00.0000+0000",
    "attendees_count": 9002
  },
  "attendees": [
    {
      "full_name": "Steve Rogers",
      "email": "blank@blank.com",
      "requires_certification": true
    },
    {
      "full_name": "Bruce Banner",
      "email": "denise.parisian@kirlin.biz",
      "requires_certification": false
    },
    {
      "full_name": "THor Odinson",
      "email": "blank@blank.com",
      "requires_certification": false
    }

标签: ruby-on-railsapipaginationkaminarijbuilder

解决方案


推荐阅读