Underscore model name in JSON while saving model

Hello everyone,

I have a model called GuestGroup. I use ActiveModelAdapter to connect to my Rails back-end. When I save the model to my backend, Ember sends the following json:

{guestGroup: {title: "test", wedding: "1"}}

As you can see, the model name is in camelCase. But I expect it to be snake_case:

{guest_group: {title: "test", wedding: "1"}}

I have 2 questions:

  1. Why ActiveModelAdapter doesn’t convert model name to snake_case? It reads underscored JSON from server without problems.
  2. How can I send my model name underscored?

There is the same question but without an answer :frowning: Serializer / Deserializer : different names (types) for this.store.find and model.save

Take a look here: Fit Any Backend Into Ember with Custom Adapters & Serializers - Ember Igniter

Specifically, you’ll want to write a serializer that converts your keys to snake_case - e.g.

// app/serializers/application.js

export default DS.RESTSerializer.extend({
  keyForAttribute: function(key) {
    // From http://emberjs.com/api/classes/Ember.String.html#method_underscore
    return Ember.String.underscore(key);
  }
});
1 Like

What ember/ember data version are you using?

Are properties being snake cased, eg { guest_group: { awesome_title: 'blah' } }?

You made need to import { ActiveModelSerializer } from 'active-model-adapter' for your application serializer.

I think he should extend from ActiveModelSerializer if that’s what he’s using. Although that should do it already:

https://github.com/ember-data/active-model-adapter/blob/master/addon/active-model-serializer.js#L119

1 Like

The problem is that I’m using ember-data-url-templates. And when I redefine my Adapter, it overrides all the methods from ActiveModelSerializer.

I’ve solved it! Thanks for your help :slight_smile: I needed to extend ActiveModelAdapter:

import UrlTemplates from "ember-data-url-templates";
import ActiveModelAdapter from 'active-model-adapter';

export default ActiveModelAdapter.extend(UrlTemplates, {
  urlTemplate: '{+host}/weddings/{weddingId}/guest_groups{/id}',
  findUrlTemplate: '{+host}/guest_groups/{id}',

  urlSegments: {
    weddingId(type, id, snapshot, query) {
      return snapshot.belongsTo('wedding', { id: true });
    }
  }
});
1 Like