Files
openfoodnetwork/app/controllers/shops_controller.rb
Pau Perez c330d931ce Eager load product properties to avoid N+1
This totally removes the following N+1 query from the GET /shops request

```sql
SELECT "spree_properties".* FROM "spree_properties"
  INNER JOIN "spree_product_properties"
    ON "spree_product_properties"."property_id" = "spree_properties"."id"
  INNER JOIN "spree_products"
    ON "spree_products"."id" = "spree_product_properties"."product_id"
  INNER JOIN "enterprises"
    ON "enterprises"."id" = "spree_products"."supplier_id"
  WHERE "spree_products"."supplier_id" = 24;
```

The product properties of the corresponding enterprises are now loaded
in a single query fired from the controller.
2018-11-28 16:21:40 +01:00

15 lines
295 B
Ruby

class ShopsController < BaseController
layout 'darkswarm'
before_filter :enable_embedded_shopfront
def index
@enterprises = Enterprise
.activated
.includes(address: :state)
.includes(:properties)
.includes(supplied_products: :properties)
.all
end
end