[英]nested query on active model serializer attribute
我有两个模型,艺术品和事件,一件艺术品有很多事件。 一些事件得到确认。
在我的序列化程序中,我想返回每个艺术品的最后确认事件
现在它确实如此,但也创建了一个缓慢的(n+1?)查询,其中每个序列化的 Artwork 获取所有事件。
我怎样才能更快地做某事?
# 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
和日志
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
你可以试试这个:
请将此关联添加到artwork
model。
# app/models/artwork.rb
has_one :last_confirmed_event, -> { order(created_at: :desc).where(confirmed: true) }, class_name: "Event"
在您的artworks
controller 中添加预先加载。
# app/controllers/artwork_controllers.rb
def index
@artworks = Artwork.all.includes(:last_confirmed_event)
render json: @artworks
end
最后在序列化程序中使用声明的关联。
# app/serializers/artwork_serializer.rb
attributes :id, :name, :photo, :created_at
has_one :last_confirmed_event
您可以根据自己的选择更改关联名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.