ActiveModel Serializer converts underscores to hyphens

Here’s the problem I ran into. I setup rails in the API mode and configured to use ActiveModelSerializers and follow the JSON5 spec. But turns out, that converts all the field names in my JSON responses to contain hyphens instead of underscores.

The serializer was converting the database field first_name into first-name in my JSON responses.

I don’t like that! I want my field names to stay the same. Untouched and pristine, like the frozen lakes of Antartica.

Turns out you can ask your serializer not not mess with your field names. Just add the following line in your active_model_serializer.rb

ActiveModel::Serializer.config.key_transform = :unaltered
Ryan Sukale

Ryan Sukale

Ryan is just a regular guy next door trying to manage his life and finances.

You may also like...

1 Response

  1. Dylan Pierce says:

    Thank you! Saved me oodles of time trying to create a custom function to keep my lovely underscores

Leave a Reply

Your email address will not be published.