Skip to content

Instantly share code, notes, and snippets.

@UsamaAshraf
Created May 23, 2021 16:52
Show Gist options
  • Save UsamaAshraf/897d3664ab8f0fc911b2939e883cd2c3 to your computer and use it in GitHub Desktop.
Save UsamaAshraf/897d3664ab8f0fc911b2939e883cd2c3 to your computer and use it in GitHub Desktop.
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :details, :author
# Will run n Mongo queries for n posts being rendered.
def author
User.find(object.author_id)
end
end
# This is now a Mongoid document, not an ActiveRecord model.
class User
include Mongoid::Document
include Mongoid::Timestamps
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment