Skip to content

Instantly share code, notes, and snippets.

@richmolj
Created September 4, 2016 19:59
Show Gist options
  • Save richmolj/961bd59a7ca521c8b80cc7b59ef87e41 to your computer and use it in GitHub Desktop.
Save richmolj/961bd59a7ca521c8b80cc7b59ef87e41 to your computer and use it in GitHub Desktop.
class ActiveModel::Serializer
def initialize(object, options = {})
@object = object
@include_directive = options[:include]
end
def as_json
hash = serialize_attributes(@object)
self.relationships.each_pair do |name, relationship|
related_record = object.send(name)
hash[:relationships][name] = relationship.serializer.new(related_record, @include_directive[name])
end
hash
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment