Created
January 27, 2019 21:25
-
-
Save Bajena/f30453a9d69013bff9b8b08f89d318fc to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
belongs_to :user | |
def self.lazy_user(post) | |
BatchLoader.for(post.user_id).batch do |user_ids| | |
User.where(id: user_ids) | |
end | |
end | |
def author | |
self.class.lazy_user(object) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment