class Comment
belongs_to :owner
belongs_to :parent # parent/children from the gem awesome_nested_set
has_many :children #
end
class Car
has_many :comments
end
所有者可以是Team
或User
或Group
。他们每个人都有name
,avatar
(carrierwave)和created_at
我们确实使用ActiveModel::Serializer
作为回复。 (CommentSerializer
)。在序列化器中,我们定义了belongs_to :owner
和has_many :children
。
render json: car.comments
这很有效,但考虑到一组100条评论(其中大多数都有儿童评论),它远远没有表现良好。所以我们可以轻松查找300多个用户/团队/组协会。
我们如何预加载它们?我正在考虑像
这样的事情 car.comments.include(:children, :owner)
但是这对于多态关联有什么用呢?
[active_model_serializers] User Load (7.0ms) SELECT `users`.* FROM `users` WHERE `users`.`deleted_at` IS NULL AND `users`.`id` = 2 LIMIT 1
[active_model_serializers] Action::Comment Load (0.4ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`deleted_at` IS NULL AND `comments`.`parent_id` = 37 ORDER BY `comments`.`lft`
[active_model_serializers] User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`deleted_at` IS NULL AND `users`.`id` = 1 LIMIT 1
[active_model_serializers] Action::Comment Load (3.1ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`deleted_at` IS NULL AND `comments`.`parent_id` = 39 ORDER BY `comments`.`lft`
[active_model_serializers] CACHE (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`deleted_at` IS NULL AND `users`.`id` = 2 LIMIT 1 [["id", 2], ["LIMIT", 1]]
[active_model_serializers] Action::Comment Load (10.6ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`deleted_at` IS NULL AND `comments`.`parent_id` = 40 ORDER BY `comments`.`lft`
[active_model_serializers] User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`deleted_at` IS NULL AND `users`.`id` = 3 LIMIT 1
[active_model_serializers] Action::Comment Load (0.4ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`deleted_at` IS NULL AND `comments`.`parent_id` = 41 ORDER BY `comments`.`lft`
[active_model_serializers] User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`deleted_at` IS NULL AND `users`.`id` = 4 LIMIT 1
[active_model_serializers] Action::Comment Load (0.4ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`deleted_at` IS NULL AND `comments`.`parent_id` = 42 ORDER BY `comments`.`lft`
[active_model_serializers] User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`deleted_at` IS NULL AND `users`.`id` = 5 LIMIT 1
[active_model_serializers] Action::Comment Load (0.4ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`deleted_at` IS NULL AND `comments`.`parent_id` = 43 ORDER BY `comments`.`lft`
[active_model_serializers] User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`deleted_at` IS NULL AND `users`.`id` = 6 LIMIT 1
[active_model_serializers] Action::Comment Load (0.4ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`deleted_at` IS NULL AND `comments`.`parent_id` = 44 ORDER BY `comments`.`lft`
[active_model_serializers] User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`deleted_at` IS NULL AND `users`.`id` = 8 LIMIT 1
[active_model_serializers] Action::Comment Load (0.6ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`deleted_at` IS NULL AND `comments`.`parent_id` = 45 ORDER BY `comments`.`lft`
[active_model_serializers] Team Load (0.5ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`id` = 2 LIMIT 1
[active_model_serializers] Action::Comment Load (0.4ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`deleted_at` IS NULL AND `comments`.`parent_id` = 61 ORDER BY `comments`.`lft`
[active_model_serializers] CACHE (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`deleted_at` IS NULL AND `users`.`id` = 2 LIMIT 1 [["id", 2], ["LIMIT", 1]]
[active_model_serializers] CACHE (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`deleted_at` IS NULL AND `users`.`id` = 1 LIMIT 1 [["id", 1], ["LIMIT", 1]]
[active_model_serializers] CACHE (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`deleted_at` IS NULL AND `users`.`id` = 2 LIMIT 1 [["id", 2], ["LIMIT", 1]]
[active_model_serializers] CACHE (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`deleted_at` IS NULL AND `users`.`id` = 3 LIMIT 1 [["id", 3], ["LIMIT", 1]]
[active_model_serializers] CACHE (0.2ms) SELECT `users`.* FROM `users` WHERE `users`.`deleted_at` IS NULL AND `users`.`id` = 4 LIMIT 1 [["id", 4], ["LIMIT", 1]]
[active_model_serializers] CACHE (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`deleted_at` IS NULL AND `users`.`id` = 5 LIMIT 1 [["id", 5], ["LIMIT", 1]]
[active_model_serializers] CACHE (0.0ms) SELECT `users`.* FROM `users` WHERE `users`.`deleted_at` IS NULL AND `users`.`id` = 6 LIMIT 1 [["id", 6], ["LIMIT", 1]]
[active_model_serializers] CACHE (5.6ms) SELECT `users`.* FROM `users` WHERE `users`.`deleted_at` IS NULL AND `users`.`id` = 8 LIMIT 1 [["id", 8], ["LIMIT", 1]]
[active_model_serializers] CACHE (0.0ms) SELECT `teams`.* FROM `teams` WHERE `teams`.`id` = 2 LIMIT 1 [["id", 2], ["LIMIT", 1]]
[active_model_serializers] Rendered ActiveModel::Serializer::CollectionSerializer with ActiveModelSerializers::Adapter::Attributes (95.65ms)
在111ms完成200 OK(浏览次数:70.3ms | ActiveRecord:34.8ms)
这实际上是总共10条评论的输出