Skip to content

Rabl doing duplicated database calls? #142

@NielsKSchjoedt

Description

@NielsKSchjoedt

I want to do something similar to this https://github.com/nesquena/rabl/wiki/Tips-and-tricks but my problem is, that it seems that rabl makes duplicated calls to the database. Since some of the request can be quite heavy (due to some geo-stuff), it's a deal breaker for me.

I have simplified the code for this example:

I'm constructing an API that should return a set of cars. I want the format to look like the following:

{

"hits_count": 2,
"cars": [
    {
        "car": {
            "id": 295655,
            "brand": "Citroën",
            "model_name": "Saxo",
            "fuel": "Diesel",
            "km": 245000,
            "year": 2002
        }
    },
    {
        "car": {
            "id": 295656,
            "brand": "Citroën",
            "model_name": "Saxo",
            "fuel": "Diesel",
            "km": 234000,
            "year": 2001
        }
    }
 ]

}

Right now I'm doing it in my /index by saying:
object false
node(:hits_count) { @cars_in_search }
child @cars do
extends "api/v1/cars/car"
end

in the /car it says:
attributes :id, :brand, :model_name, :fuel, :km, :year

the @cars is a query selecting the cars, and the @cars_in_search is just a simple count

This gives me the correct format as I want, but it makes duplicate calls to the database as you can see:
(238.7ms) SELECT COUNT() FROM "cars" WHERE "cars"."sales_state" = 'onsale' AND "cars"."estimate_state" = 'valid'
(1.2ms) SELECT COUNT(count_column) FROM (SELECT 1 AS count_column FROM "cars" WHERE "cars"."sales_state" = 'onsale' AND "cars"."estimate_state" = 'valid' LIMIT 20 OFFSET 0) subquery_for_count
Car Load (281.4ms) SELECT "cars".
FROM "cars" WHERE "cars"."sales_state" = 'onsale' AND "cars"."estimate_state" = 'valid' ORDER BY cars.au_rating DESC, cars.price_difference DESC LIMIT 1 OFFSET 0
Car Load (291.7ms) SELECT "cars".* FROM "cars" WHERE "cars"."sales_state" = 'onsale' AND "cars"."estimate_state" = 'valid' ORDER BY cars.au_rating DESC, cars.price_difference DESC LIMIT 20 OFFSET 0

If instead I do the following in my /index view:
collection @cars
extends "api/v1/cars/car"

Then it's not doing duplicate requests ?????? strange:
(228.6ms) SELECT COUNT() FROM "cars" WHERE "cars"."sales_state" = 'onsale' AND "cars"."estimate_state" = 'valid'
Car Load (286.9ms) SELECT "cars".
FROM "cars" WHERE "cars"."sales_state" = 'onsale' AND "cars"."estimate_state" = 'valid' ORDER BY cars.au_rating DESC, cars.price_difference DESC LIMIT 20 OFFSET 0

But then the problem of cause is, that I'm not getting the format that I want (as described before)

Hope you can give me a clue as to what to do.

I'm running rails 3.1 and latest rabl. It's the same in both development and production.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions