Skip to content

Conversation

@nikolai-b
Copy link

as_json does not work with the newest version of https://github.com/rails-api/active_model_serializers and it is suggested that you use the ActiveModel::SerializableResource class.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

coming here from: rails-api/active_model_serializers#1321 (comment)

could be a little more efficient by just defining a different method per version

if ActiveModel::Serializer::VERSION.start_with?('0.9.')
        def serializable_hash(options = {})
          instance = serializer.new(object, options)
          if instance.respond_to?(:serializable_hash)
            instance.serializable_hash
          else
            instance.as_json options
          end
        end
elsif ActiveModel::Serializer::VERSION.start_with?('0.10.')
  defined?(ActiveModel::SerializableResource) or fail "Your ActiveModelSerializers is too old. Please update to the latest minor or patch release."
  def serializable_hash(options = {})
    ActiveModel::SerializableResource.new(object, options.merge(serializer: serializer)).serializable_hash
  end
else
  fail ArgumentError, "We don't know how to serialize in #{ActiveModel::Serializer::VERSION}"
end

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This stems partly from the super loose constraint in the Gemfile

gem 'active_model_serializers', ENV['AMS_VERSION'] || '> 0.0'

(I'm guessing it's a soft dependency?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants