Search code examples
ruby-on-railsactive-model-serializers

Polymorphic association with AMS using JSON:API specification


I have been trying save a object who has a polymorphic association, take a look at my create_params:

module Admin
  class ChatroomsController < AdminController # :nodoc:
    ...

    def create_params
      ActiveModelSerializers::Deserialization.jsonapi_parse!(
        params,
        only: [:name, :chatable],
        polymorphic: [:chatable]
      )
    end
  end
end

When send to save, the AMS not find a way to resolve the model name:

#<NameError: wrong constant name disputes>

How I can save objects with polymorphic associations? Thanks.


Solution

  • I used the follow fix to solve my issue:

    module Admin
      class ChatroomsController < AdminController # :nodoc:
        ...
    
        def create_params
          res = ActiveModelSerializers::Deserialization.jsonapi_parse!(
            params,
            only: [:name, :chatable],
            polymorphic: [:chatable]
          )
          res[:chatable_type] = res[:chatable_type].singularize.capitalize
          res
        end
      end
    end
    

    AMS have a pull request to solve this.