如何在我的 json 端点中获取完整数据?

How can I the full data in my json endpoint?

我为我的 Rails 应用程序设置了一个简单的 json api 端点。我有一个属于另一个模型 List 的模型 Item,我想显示属于特定 List 的所有 Items。然而,在 json 文档突然结束之前,实际只显示了 606 Items。是否可以以某种方式指定端点显示更多数据,或者这是限制?

def endpoint
  list = List.find_by(name: params[:list])
  respond_to do |format|
    format.html
    format.json {render json: list.items}
    # this list has thousands of items but only 606 are displayed.
  end
end

这是服务器中的输出:

Called from /home/user/.rvm/gems/ruby-2.3.3/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
  ActiveRecord::SchemaMigration Load (6.2ms)  SELECT "schema_migrations".* FROM "schema_migrations"
Processing by AdminsController#endpoint as JSON
  Parameters: {"list"=>"Double"}
  List Load (1.5ms)  SELECT  "lists".* FROM "lists" WHERE "lists"."name" =  LIMIT   [["name", "Double"], ["LIMIT", 1]]
  Item Load (228.4ms)  SELECT "lists".* FROM "items" WHERE "items"."list_id" =   [["list_id", 34]]
Completed 200 OK in 5856ms (Views: 5215.1ms | ActiveRecord: 294.5ms)

我开发了一个 REST API 应用程序,我已经完成了很多事情,但没有遇到那种类型的问题,我正在编写下面的代码,我用这种方式获取数据,如下所示

def endpoint
    @list = List.find_by(name: params[:list])
    @list_items = @list.items.limit(100) #=> limit is how much you need, I have not so much data for testing like 10,000
    respond_to do |format|
        format.html
        format.json {@list_items}
    end

    # OR

    json_response(@list_items) #=> without respond_to block
end

希望有所帮助

这将减少查询所花费的时间:

def endpoint
    list = List.includes(:items).find_by(name: params[:list]).as_json(includes: :items)

    respond_to do |format|
        format.html
        format.json {list}
    end
end

希望这对你有用