Today I Learned

Singleton can't be dumped error

when passing an array of objects to a serializer, I’ve received an error which stated: Singleton can't be dumped. To fix the issue I’ve created a presenter for a singleton object:

  class StateTransitionPresenter
    attr_reader :state_transition

    def initialize(state_transition)
      @state_transition = state_transition
    end

    def as_json(options = {})
      {
        state_transition_time: state_transition.time,
        state: state_transition.state,
        reason: state_transition.reason
      }
    end
  end

and then I used it in the serializer:

order.state_transitions.map{ |state_transition| StateTransitionPresenter.new(state_transition)}.as_json