Last active
May 23, 2021 16:42
-
-
Save UsamaAshraf/95b0c8d0d64ee193148342a931c0a423 to your computer and use it in GitHub Desktop.
N+1 Queries, Batch Loading and Active Model Serializers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ... | |
# https://github.com/exAspArk/batch-loader | |
gem 'batch-loader' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostsController < ApplicationController | |
def index | |
posts = Post.all | |
render json: posts | |
end | |
end | |
class Post | |
belongs_to :author, class_name: 'User' | |
end | |
class PostSerializer < ActiveModel::Serializer | |
attributes :id, :title, :details | |
belongs_to :author | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Post < ApplicationRecord | |
# ... | |
def get_author_lazily | |
BatchLoader.for(self).batch do |posts, batch_loader| | |
User.where(:_id.in => posts.pluck(:author_id)).each do |user| | |
# Modify the user through a given block, say, for serialization. | |
modified_user = block_given? ? yield(user) : user | |
batch_loader.call(posts.detect { |p| p.author_id == user._id.to_s }, modified_user) | |
end | |
end | |
end | |
# ... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostsController < ApplicationController | |
def index | |
# Can't do Post.includes(:author) beacuse the author (User object) | |
# is stored in an entirely different database: a MongoDB instance. | |
posts = Post.all | |
render json: posts | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PostSerializer < ActiveModel::Serializer | |
attributes :id, :title, :details, :author | |
def author | |
object.get_author_lazily do |author| | |
# Serialize the author after it has been loaded. | |
ActiveModelSerializers::SerializableResource.new(author).as_json[:user] | |
end | |
end | |
# ... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
# ... | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserSerializer < ActiveModel::Serializer | |
# .... | |
end |
Hey, I digged in this topic a bit and created a plugin for ActiveModelSerializers - https://github.com/Bajena/ams_lazy_relationships
It eliminates the problem that @lucasmaso mentioned in his comment :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@luccasmaso yes,
include
works only for ORM-defined database relations, not custom attributes, which is whatuser
has become in our case.We can pass custom parameters and access them with
@instance_options
to achieve our goal:You can also stick with
include
, use@instance_options[:include]
and check ifuser
was specified. But I'd probably not do this because it sort of goes against what theinclude
option is supposed to be for. Also, in a way our point was to avoidinclude
since it forced the n+1 queries to run. Having said that, there's nothing essentially wrong with using@instance_options[:include]
.As far as nested associations are concerned, you can pass them to the explicit call to AMS;
Sorry for replying late. Don't know why I didn't get an email!