Notes to self

How to keep hash keys untouched by ActiveModel::Serializer

At work we use ActiveModel::Serializer to serialize ActiveRecord objects into JSON:API-like responses for our APIs. The problem is that ActiveModel::Serializer is clever enough to dasherize our hash keys (so they can be accessed as properties instead of [] brackets syntax) and our hash keys are actually real keys that we have to serve untouched.

Luckily the solution is more than simple. Insert the following setting into AMS initializer and you are done:

ActiveModelSerializers.config.key_transform = :unaltered

:unaltered have also the added benefit of being faster as it does not modify anything. I believe it should be default, perhaps it was, I am on version 0.10.5.

Work with me

I have some availability for contract work. I can be your fractional CTO, a Ruby on Rails engineer, or consultant. Write me at strzibny@strzibny.name.

RSS