# Relationship id return to server a String and not a Int!

**URL:** <https://discuss.emberjs.com/t/relationship-id-return-to-server-a-string-and-not-a-int/8467>\
**Category:** Ember Data\
**Created:** [July 29, 2015, 7:49pm UTC](https://discuss.emberjs.com/t/relationship-id-return-to-server-a-string-and-not-a-int/8467 "2015-07-29T19:49:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ErunamoJAZZ](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/erunamojazz/32/10932_2.png) [@ErunamoJAZZ](https://discuss.emberjs.com/u/ErunamoJAZZ)\
**Post date:** [July 29, 2015, 7:49pm UTC](https://discuss.emberjs.com/t/relationship-id-return-to-server-a-string-and-not-a-int/8467/1 "2015-07-29T19:49:07Z")

</div>

Hello!

Im a new ember-data user, this is too interesting (although documentation feels very dispersed yet), and I have a problem now. Im trying to port an ember project from jQuery to ember-data, and my model have a common relationship like this:

```auto
//entity
export default DS.Model.extend({
  name: DS.attr('string'),
});
    
//user
export default DS.Model.extend({
  email: DS.attr('string'),
  entity: DS.belongsTo('entity',{ async: true }),
});

```

Now, when i create a new user, all is okay in client side ( using by example `this.store.peekRecord('entity',1)` to choose the entity before `this.store.createRecord`), but on `save()` this fails because entity attr arrived to server like a String and not as Integer (`"entity":"1"`).

I read in [https://github.com/rykov/ember-data/blob/master/BREAKING\_CHANGES.md](https://github.com/rykov/ember-data/blob/master/BREAKING_CHANGES.md) (revision 6), this is the default behaviour, but i dont want this.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![ErunamoJAZZ](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/erunamojazz/32/10932_2.png) [@ErunamoJAZZ](https://discuss.emberjs.com/u/ErunamoJAZZ)\
**Post date:** [July 30, 2015, 9:53pm UTC](https://discuss.emberjs.com/t/relationship-id-return-to-server-a-string-and-not-a-int/8467/2 "2015-07-30T21:53:10Z")

</div>

Okay, I tried it on my own using a serializer object:

```auto
export default DS.RESTSerializer.extend({
    serialize: function(snapshot, options) {
        var json = this._super(snapshot, options);
        json.entity = parseInt(json.entity);
        return json;
    }
});

```

However, I do not know if this is the best solution … should also be noted that the documentation of code seems out of date, since the id displayed as numbers and not as strings.

😉

---

<div class="post-metadata">

**Author:** ![Lysergia](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/lysergia/32/12121_2.png) [@Lysergia](https://discuss.emberjs.com/u/Lysergia)\
**Post date:** [March 8, 2016, 12:24pm UTC](https://discuss.emberjs.com/t/relationship-id-return-to-server-a-string-and-not-a-int/8467/3 "2016-03-08T12:24:09Z")

</div>

Ran into this problem when using Ember w/ Yesod, my solution was to modify the application serializer to call `parseInt` for all relationships using `serializeBelongsTo` and `seriailizeHasMany`. This way you do not have to write a serializer for each model, or a `parseInt` call for every attribute.

```auto
import DS from 'ember-data';

export default DS.RESTSerializer.extend({
  serializeBelongsTo: function(record, json, relationship) {
    this._super(record, json, relationship);
    var key = relationship.key;
    var json_key = this.keyForRelationship ? this.keyForRelationship(key, "belongsTo") : key;
    json[json_key] = parseInt(json[json_key]);
  },
  serializeHasMany: function(record, json, relationship) {
    this._super(record, json, relationship);
    var key = relationship.key;
    var json_key = this.keyForRelationship ? this.keyForRelationship(key, "hasMany") : key;
    json[json_key] = parseInt(json[json_key]);
  }
});

```

---

<div class="post-metadata">

**Author:** ![huberts](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/huberts/32/11743_2.png) [@huberts](https://discuss.emberjs.com/u/huberts)\
**Post date:** [March 29, 2016, 7:06am UTC](https://discuss.emberjs.com/t/relationship-id-return-to-server-a-string-and-not-a-int/8467/4 "2016-03-29T07:06:15Z")

</div>

I really liked your response, but I think it needs a small improvement - one needs to iterate over hasMany attribute to collect all values (following code is written in CoffeeScript):

```
serializeHasMany: (snapshot, json, relationship) ->
  @_super snapshot, json, relationship
  key = @keyForRelationship relationship.key, "hasMany" # with provided keyForRelationship() implementation
  for value, index in json[key]
    json[key][index] = parseInt value

```

---

<div class="post-metadata">

**Author:** ![Lysergia](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/lysergia/32/12121_2.png) [@Lysergia](https://discuss.emberjs.com/u/Lysergia)\
**Post date:** [August 11, 2016, 4:33am UTC](https://discuss.emberjs.com/t/relationship-id-return-to-server-a-string-and-not-a-int/8467/5 "2016-08-11T04:33:09Z")

</div>

Thanks, quite a bit late, but I ended up with this:

```javascript
import DS from 'ember-data';

export default DS.RESTSerializer.extend({
  serializeBelongsTo: function(record, json, relationship) {
    this._super(record, json, relationship);
    var key = relationship.key;
    var json_key = this.keyForRelationship ? this.keyForRelationship(key, "belongsTo") : key;
    json[json_key] = parseInt(json[json_key]);
  },
  serializeHasMany: function(record, json, relationship) {
    this._super(record, json, relationship);
    var key = relationship.key;
    var json_key = this.keyForRelationship ? this.keyForRelationship(key, "hasMany") : key;
    if (json_key in json) {
      json[json_key] = json[json_key].map((item) => {
        parseInt(json[json_key]);
      });
    }
  }
}); 

```
