Skip to content

Instantly share code, notes, and snippets.

@Bajena
Created January 27, 2019 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bajena/f30453a9d69013bff9b8b08f89d318fc to your computer and use it in GitHub Desktop.
Save Bajena/f30453a9d69013bff9b8b08f89d318fc to your computer and use it in GitHub Desktop.
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