Rails: Active Model Serializers

Configuring Format for Keys

For the whole project

config/initializers/active_model_serializer.rb

ActiveModel::Serializer.setup do |config|
  config.key_format = :lower_camel
end

For an individual serializer or a base serializer

Currently, only lower_camel is supported. If we want none just put format_keys :none

class BlogLowerCamelSerializer < ActiveModel::Serializer
  format_keys :lower_camel
end

https://github.com/rails-api/active_model_serializers/pull/534

Possibly a new addition

https://github.com/rails-api/active_model_serializers/pull/1029

Formats

In the works are multiple types of JSON formats. However, some libraries (like https://github.com/google/google-api-ruby-client) abstract out their interface for hashes. So, to convert to a hash, we can just parse the json.

JSON.parse(MyModelSerializer.new(my_model).to_json)

Or we can add a base class

class BaseSerializer < ActiveModel::Serializer
    def hashed
      JSON.parse(self.to_json)
    end
end

# Then call it like
MyModelSerializer.new(my_model).hashed

blog comments powered by Disqus