Search code examples
ruby-on-railsjsonrestapi-designendpoint

How can I the full data in my json endpoint?


I set up a simple json api endpoint for my Rails application. I have a model Item that belongs to another model List, and I want to display all the Items that belong to a particular List. However, only 606 Items are actually displayed before the json document abruptly ends. Is it possible to somehow specify that the endpoint display more data, or is that the limit?

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

Here's the output in the server:

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)

Solution

  • This will reduce the time taking by query:

    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
    

    Hope this will work for you