Search code examples
ruby-on-railsember.jsember-dataactive-model-serializers

How translate serialize type with dash to model slashed structure?


My API in Rails with AMS (JSON:API) return from serializer the type name with dash (artemis-forum-disputes), but inside my frontend app, who uses Ember, I store my models with subdirectory structure (artemis/forum/disputes).

WARNING: Encountered a resource object with type "artemis-forum-disputes", but no model was found for model name "artemis-forum-dispute" (resolved model name using 'apollo-enterprise@serializer:application:.modelNameFromPayloadKey("artemis-forum-disputes")').

How solve this? Thanks.


Solution

  • You can tell Ember Data which model to use by overriding the modelNameFromPayloadType method on your serializer. If you override that method in your application serializer and have it transform the dashes to slashes Ember should be able to find your models in a subdirectory.

    // app/serializers/application.js 
    // or app/application/serializer.js
    import DS from 'ember-data'
    export default DS.JSONAPISerializer.extend({
      modelNameFromPayloadType(payloadType) {
        return payloadType.replace(/-/g, '/');
      }
    });