I have two models, Artwork and Event, an artwork has many events. Some events are confirmed.
In my serializer I want to return the last confirmed event for each Artwork
Right now it does but also creates a slow (n+1 ?) query where each serialized Artwork fetches all events.
How can I do something faster ?
# artwork_controller.rb
def index
@artworks = Artwork.all
render json: @artworks
end
# artwork_serializer.rb
attributes :id, :name, :photo, :created_at
has_one :event do
object.events.confirmed.last
end
and logs
Processing by Api::V1::ArtworksController#index as JSON
Passenger Load (2.0ms) SELECT "artworks".* FROM "artworks"
↳ app/controllers/api/v1/artworks_controller.rb:11
[active_model_serializers] Event Load (1.2ms) SELECT "events".* FROM "events" WHERE "events"."artwork_id" = $1 AND "events"."published" = $2 ORDER BY "events"."created_at" DESC LIMIT $3 [["artwork_id", 16], ["published", true], ["LIMIT", 1]]
[active_model_serializers] ↳ app/serializers/api/v1/artwork_serializer.rb:9
[active_model_serializers] Event Load (0.8ms) SELECT "events".* FROM "events" WHERE "events"."artwork_id" = $1 AND "events"."published" = $2 ORDER BY "events"."created_at" DESC LIMIT $3 [["artwork_id", 9], ["published", true], ["LIMIT", 1]]
[active_model_serializers] ↳ app/serializers/api/v1/artwork_serializer.rb:9
[active_model_serializers] Event Load (2.1ms) SELECT "events".* FROM "events" WHERE "events"."artwork_id" = $1 AND "events"."published" = $2 ORDER BY "events"."created_at" DESC LIMIT $3 [["artwork_id", 27], ["published", true], ["LIMIT", 1]]
[active_model_serializers] ↳ app/serializers/api/v1/artwork_serializer.rb:9#
# ... for each artwork
[active_model_serializers] ↳ app/serializers/api/v1/artwork_serializer.rb:9
[active_model_serializers] User Load (1.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 84], ["LIMIT", 1]]
[active_model_serializers] ↳ app/serializers/api/v1/event_serializer.rb:9
[active_model_serializers] CACHE Event Load (0.0ms) SELECT "events".* FROM "events" WHERE "events"."artwork_id" = $1 AND "events"."published" = $2 ORDER BY "events"."created_at" DESC LIMIT $3 [["artwork_id", 9], ["published", true], ["LIMIT", 1]]
[active_model_serializers] ↳ app/serializers/api/v1/artwork_serializer.rb:9
[active_model_serializers] User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 88], ["LIMIT", 1]]
[active_model_serializers] ↳ app/serializers/api/v1/event_serializer.rb:9
[active_model_serializers] CACHE Event Load (0.0ms) SELECT "events".* FROM "events" WHERE "events"."artwork_id" = $1 AND "events"."published" = $2 ORDER BY "events"."created_at" DESC LIMIT $3 [["artwork_id", 27], ["published", true], ["LIMIT", 1]]
[active_model_serializers] ↳ app/serializers/api/v1/artwork_serializer.rb:9
[active_model_serializers] User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 76], ["LIMIT", 1]]
[active_model_serializers] ↳ app/serializers/api/v1/event_serializer.rb:9
[active_model_serializers] CACHE Event Load (0.0ms) SELECT "events".* FROM "events" WHERE "events"."artwork_id" = $1 AND "events"."published" = $2 ORDER BY "events"."created_at" DESC LIMIT $3 [["artwork_id", 11], ["published", true], ["LIMIT", 1]]
# ... for each artwork events
you can try this:
Please add this association to the artwork
model.
# app/models/artwork.rb
has_one :last_confirmed_event, -> { order(created_at: :desc).where(confirmed: true) }, class_name: "Event"
In your artworks
controller add eager-loading.
# app/controllers/artwork_controllers.rb
def index
@artworks = Artwork.all.includes(:last_confirmed_event)
render json: @artworks
end
At the last use declared association in your serializer.
# app/serializers/artwork_serializer.rb
attributes :id, :name, :photo, :created_at
has_one :last_confirmed_event
You can change the association name as per your choice.