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.
I think one of my larger problems--once I realized that I had to include
ActiveModel::SerializerSupport
--is that I don't like having to overload the association-style methods this way, e.g. calling.has_one :pusher
followed bydef pusher; ...
seems too obvious a kludge. But this is clearly the legacy of ActiveModel::Serializers being designed first and foremost to serialize normal-rails-style models.So basically: I'm looking for a third sort of method beyond
attributes
andhas_(one|many)
that will allow me to specify additional output to be included in the serialization.