App has 4 keys in a hash property called stats
: '10s', '1min', '5min', 'day'. Each are arrays of Stat objects
In order to create a stats
attribute in AppSerializer, we have to create an
intermediary Serializer with the attributes in order to re-use StatSerializer
for each of those four Stat collections, and the syntax within that Serializer
is pretty kludgy to make the association work.
Using ActiveModel::Serializer outside of ActiveRecord requires an inclusion of ActiveModel::SerializerSupport, but the documentation does not state this, so there was some head-scratching as to why it wasn't magically working on our Mongoid models.
I find instances where I have a hash attribute that I need to specify keys of the hash that I either need to write a filtered hash object or its own Serializer object. That may or may not be working-as-intended as this has some feeling of hash-driven mentality, but it surprised me.
I cannot find a way to make the serializer data be the root of the JSON response, instead of being the data in a key. For example, if instead of wanting { 'app': { 'foo': 'bar' } } I want { 'foo': 'bar' }, there does not seem to be a way for that. I can change 'app' to 'foo' by passing the :root option, but I do not see a way to cause it to be the root of the JSON response itself.
Por ejemplo, when I was rolling my own responders for scenarios like the first pair of examples here, I would have a method something like this: