You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a Location model that has_one :address. The data is stored in Postgres. Here's the Grape API endpoint definition for fetching all locations:
resource"locations"do# GET /locationsdesc'Returns all locations, 30 per page by default'paramsdooptional:page,type: Integer,default: 1optional:per_page,type: Integer,default: 30endgetdolocations=Location.includes(:address).page(params[:page]).per(params[:per_page])presentlocations,with: Entities::Locationendend
Note that there are two calls to each table: one to fetch the first record in the results, and then another to fetch all the results (up to the per_page amount).
If I change the endpoint definition to just return locations without any grape-entity representation, and instead, use my own as_json method in the Locations model, like this:
This is reproducible every time I switch back and forth between the two methods of representing the JSON. It also happens if the JSON doesn't include data from other tables, and even when you're just calling Location.all (not that you would do that). It always makes 2 calls to the Locations table.
In the actual app, I need to include 8 other tables, so having an extra call to each one adds up!
Has anyone noticed this before? Is there a way to write a spec for this?
The text was updated successfully, but these errors were encountered:
@monfresh & @dblock You'r using a scope as parameter for present. That's the issue. It arises within grape. To get the Class of you'r Collection Instance Grape is looking at the first element. With rails this is an issue. You can walk around this by explicitly setting the Entity class or you explicitly use .load
I have a
Location
model thathas_one :address
. The data is stored in Postgres. Here's the Grape API endpoint definition for fetching all locations:My (simplified) Location Entities:
The Address::Entity is defined within the Address model:
When I visit http://localhost:8080/api/locations, I get the following output in the log:
Note that there are two calls to each table: one to fetch the first record in the results, and then another to fetch all the results (up to the per_page amount).
If I change the endpoint definition to just return
locations
without any grape-entity representation, and instead, use my ownas_json
method in the Locations model, like this:then, I only end up with one call to each table, as opposed to the two that grape-entity made:
This is reproducible every time I switch back and forth between the two methods of representing the JSON. It also happens if the JSON doesn't include data from other tables, and even when you're just calling
Location.all
(not that you would do that). It always makes 2 calls to the Locations table.In the actual app, I need to include 8 other tables, so having an extra call to each one adds up!
Has anyone noticed this before? Is there a way to write a spec for this?
The text was updated successfully, but these errors were encountered: