# Changes with DS.RESTAdapter?

**URL:** <https://discuss.emberjs.com/t/changes-with-ds-restadapter/2406>\
**Category:** Ember Data\
**Created:** [September 3, 2013, 5:39pm UTC](https://discuss.emberjs.com/t/changes-with-ds-restadapter/2406 "2013-09-03T17:39:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![efigarolam](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/efigarolam/32/4728_2.png) [@efigarolam](https://discuss.emberjs.com/u/efigarolam)\
**Post date:** [September 3, 2013, 5:39pm UTC](https://discuss.emberjs.com/t/changes-with-ds-restadapter/2406/1 "2013-09-03T17:39:39Z")

</div>

Hi!

I have the following model

```
App.User = DS.Model.extend
  name: DS.attr('string')
  lastName: DS.attr('string')
  email: DS.attr('string')
  admin: DS.attr('boolean')
  active: DS.attr('boolean')
  posts: DS.hasMany('post')
  comments: DS.hasMany('comment')

```

My json returns this:

```
{
  "id": 1,
  "name": "Eduardo",
  "last_name": "Figarola",
  "email": "myemail@gmail.com",
  "admin": true,
  "active": true,
  "comments": []
}

```

When I try to do this:

```
user.get('lastName')

```

I get a null value. But if I do something like this:

```
user.get('_data.last_name')

```

I see the proper value, is there a new naming convention for model attributes and JSON api? I want to highlight that the previous code was working before I updated to Ember.js 1 and Ember Data 1.0.0-beta.1

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![efigarolam](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/efigarolam/32/4728_2.png) [@efigarolam](https://discuss.emberjs.com/u/efigarolam)\
**Post date:** [September 3, 2013, 5:57pm UTC](https://discuss.emberjs.com/t/changes-with-ds-restadapter/2406/2 "2013-09-03T17:57:58Z")

</div>

I just changed the Rails serializer to return the following JSON:

```
 {
   "id": 1,
   "name": "Eduardo",
   "lastName": "Figarola Mota",
   "email": "lalo.diabulux@gmail.com",
   "admin": true,
   "active": true,
   "comments": []
 }

```

And it worked, is this the new convention?

---

<div class="post-metadata">

**Author:** ![seif](https://avatars.discourse-cdn.com/v4/letter/s/3be4f8/32.png) [@seif](https://discuss.emberjs.com/u/seif)\
**Post date:** [September 4, 2013, 4:55pm UTC](https://discuss.emberjs.com/t/changes-with-ds-restadapter/2406/3 "2013-09-04T16:55:21Z")

</div>

Kinda. Before the beta, the documentation was saying that you should use camel case, but if you called it using underscore, it worked too. But now they just handle camel case.

---

<div class="post-metadata">

**Author:** ![nragaz](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/nragaz/32/14370_2.png) [@nragaz](https://discuss.emberjs.com/u/nragaz)\
**Post date:** [September 4, 2013, 5:07pm UTC](https://discuss.emberjs.com/t/changes-with-ds-restadapter/2406/4 "2013-09-04T17:07:50Z")

</div>

@wycats added some stuff to TRANSITION.md to explain this case, sorta. Basically, you need to subclass the RESTSerializer and modify it to use the old conventions:

```
ActiveModelSerializer = DS.RESTSerializer.extend
  # Camelize keys and accept _id and _ids for associations.
  normalize: (type, property, hash) ->
    normalized = {}

    for own prop, value of hash
      if prop.substr(-3) is '_id' # belongsTo relationships
        normalizedKey = prop.slice(0, -3)
      else if prop.substr(-4) is '_ids' # hasMany relationship
        normalizedKey = Ember.String.pluralize(prop.slice(0, -4))
      else # regular attribute
        normalizedKey = prop

      normalizedKey = Ember.String.camelize(normalizedKey)
      normalized[normalizedKey] = value

    @_super(type, property, normalized)

  # Underscore keys and use _id and _ids for associations.
  serialize: (record, options) ->
    json = {}

    record.eachAttribute (name) ->
      json[name.underscore()] = record.get(name)

    record.eachRelationship (name, relationship) ->
      if relationship.kind is 'hasMany'
        key = name.singularize().underscore() + '_ids'
        json[key] = record.get(name).mapBy('id')
      else
        key = name.underscore() + '_id'
        json[key] = record.get(name + '.id')

    json.id = record.get('id') if options?.includeId

    json

App = Em.Application.create({ ApplicationSerializer: ActiveModelSerializer })

```

You also need to do something similar with the RESTAdapter to ensure that your API is called using underscore\_names consistently:

```
ActiveModelAdapter = DS.RESTAdapter.extend
  namespace: {{whatever}}

  # Always use underscore_names in URLs.
  rootForType: (type) ->
    decamelized = Em.String.decamelize(type)
    Em.String.pluralize(decamelized)

App.ApplicationAdapter = ActiveModelAdapter

```

Note that this code is for the current HEAD, not the shipped release.

---

<div class="post-metadata">

**Author:** ![matthooks](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/matthooks/32/14936_2.png) [@matthooks](https://discuss.emberjs.com/u/matthooks)\
**Post date:** [September 4, 2013, 7:13pm UTC](https://discuss.emberjs.com/t/changes-with-ds-restadapter/2406/5 "2013-09-04T19:13:24Z")

</div>

@nragaz Thanks for these code snippets!

@wycats @tomdale What are we expecting the default to be here? If we want the default to be sans \_id(s) then maybe we should make that the default in active\_model\_serializers as well, assuming that’s still the preferred way to produce JSON for ember via Ruby/Rails. On a side note, it seems like development on AMS has stagnated waiting for spastorino to finish refactoring on [his fork](https://github.com/spastorino/active_model_serializers).

---

<div class="post-metadata">

**Author:** ![jherdman](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/jherdman/32/15000_2.png) [@jherdman](https://discuss.emberjs.com/u/jherdman)\
**Post date:** [September 5, 2013, 1:35am UTC](https://discuss.emberjs.com/t/changes-with-ds-restadapter/2406/6 "2013-09-05T01:35:33Z")

</div>

A few notes for the other folks new to Ember:

1. Instead of creating my own “ActiveModelSerializer”, I took @nragaz code and did the following:

2. Instead of creating my own ActiveModelAdapter, I merely reopened the DS.RESTAdapter.

The combination of the two steps got me back up and running. Whether or not this is best practice I have to defer to the more experienced on this forum.

---

<div class="post-metadata">

**Author:** ![nragaz](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/nragaz/32/14370_2.png) [@nragaz](https://discuss.emberjs.com/u/nragaz)\
**Post date:** [September 5, 2013, 2:14am UTC](https://discuss.emberjs.com/t/changes-with-ds-restadapter/2406/7 "2013-09-05T02:14:57Z")

</div>

The signature for the normalize method changed tonight, so here’s an updated snippet:

```
ActiveModelSerializer = DS.RESTSerializer.extend
  # Camelize keys and accept _id and _ids for associations.
  normalize: (type, hash, property) ->
    normalized = {}

    for own prop, value of hash
      if prop.substr(-3) is '_id' # belongsTo relationships
        normalizedKey = prop.slice(0, -3)
      else if prop.substr(-4) is '_ids' # hasMany relationship
        normalizedKey = Ember.String.pluralize(prop.slice(0, -4))
      else # regular attribute
        normalizedKey = prop

      normalizedKey = Ember.String.camelize(normalizedKey)
      normalized[normalizedKey] = value

    @_super(type, normalized, property)

  # Underscore keys and use _id and _ids for associations.
  serialize: (record, options) ->
    json = {}

    record.eachAttribute (name) ->
      json[name.underscore()] = record.get(name)

    record.eachRelationship (name, relationship) ->
      if relationship.kind is 'hasMany'
        key = name.singularize().underscore() + '_ids'
        json[key] = record.get(name).mapBy('id')
      else
        key = name.underscore() + '_id'
        json[key] = record.get(name + '.id')

    json.id = record.get('id') if options?.includeId

    json

ActiveModelAdapter = DS.RESTAdapter.extend
  namespace: {{your namespace}}

  rootForType: (type) ->
    decamelized = Em.String.decamelize(type)
    Em.String.pluralize(decamelized)

App = Em.Application.create
  ApplicationAdapter: ActiveModelAdapter
  ApplicationSerializer: ActiveModelSerializer

```

@jherdman I don’t think it matters which way you go – I like to define the adapter and serializer separately, but that’s just a preference.

---

<div class="post-metadata">

**Author:** ![yyy](https://avatars.discourse-cdn.com/v4/letter/y/ebca7d/32.png) [@yyy](https://discuss.emberjs.com/u/yyy)\
**Post date:** [September 5, 2013, 6:14pm UTC](https://discuss.emberjs.com/t/changes-with-ds-restadapter/2406/8 "2013-09-05T18:14:13Z")

</div>

@nragaz thanks. You may want to make input to Bradley Priest’s pull request:

Basic DS.ActiveModelSerializer implementation

[https://github.com/emberjs/data/pull/1206](https://github.com/emberjs/data/pull/1206)

---

<div class="post-metadata">

**Author:** ![nikolas](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/nikolas/32/15158_2.png) [@nikolas](https://discuss.emberjs.com/u/nikolas)\
**Post date:** [December 13, 2013, 10:38pm UTC](https://discuss.emberjs.com/t/changes-with-ds-restadapter/2406/9 "2013-12-13T22:38:04Z")

</div>

@yyy Thanks for pointing that out! For my backend I just needed to extend DS.ActiveModelSerializer instead of DS.RESTSerializer - no need to manually configure camelization, etc.
