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

时间:2018-01-10 05:57:45

标签: ruby-on-rails json rest api-design endpoint

我为我的Rails应用程序设置了一个简单的json api端点。我有一个属于另一个模型Item的模型List,我希望显示属于特定Items的所有List。但是,在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" = $1 LIMIT $2  [["name", "Double"], ["LIMIT", 1]]
  Item Load (228.4ms)  SELECT "lists".* FROM "items" WHERE "items"."list_id" = $1  [["list_id", 34]]
Completed 200 OK in 5856ms (Views: 5215.1ms | ActiveRecord: 294.5ms)

2 个答案:

答案 0 :(得分:1)

我已经完成了一个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

希望能提供帮助

答案 1 :(得分:1)

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

javax/enterprise/context/spi/Contextual:

希望这对你有用