Embed nil has_one relationships in root as empty array #501

Closed
joefiorini opened this Issue Feb 5, 2014 · 6 comments

Projects

None yet

5 participants

@joefiorini

I noticed Ember's ActiveModelAdapter fails when a serializer sideloads a has_one relationship that is nil. It's sideloading as null rather than [], for eg.

{
  "user": {
    "name": "Name 1",
    "profile_id": null
  },
  "profiles": null
}

should be:

{
  "user": {
    "name": "Name 1",
    "profile_id": null
  },
  "profiles": []
}

I have a simple fix for this in a fork, I'll open a PR momentarily.

@carsomyr

+1, looks like I'm having the same problem. I monkey patched the following line like so.

(hash[key] ||= []).concat(serialized_data || []).uniq!
@mrloop
mrloop commented Feb 21, 2014

I bumped into this problem as well, here's the error message I was getting for anybody else searching for it

TypeError: Array.prototype.forEach called on null or undefined
    at forEach (native)
    at JSONSerializer.extend.extractSingle
    at superWrapper
    at Enquirli.ApplicationSerializer.DS.ActiveModelSerializer.extend.extractSingle

This is on Ember Data 1.0.0-beta.7 and 2792210

Instead of patching rubys ActiveModelSerializer I extend ember datas ActiveModelSerializer to fix the problem

App.ApplicationSerializer = DS.ActiveModelSerializer.extend
  extractSingle: (store, primaryType, payload, recordId, requestType) ->
    for key, value of payload
      delete payload[key] if value == null
    @_super(store, primaryType, payload, recordId, requestType)
@MSch
MSch commented Mar 8, 2014

Bumped into this too. Thanks @joefiorini

@MSch
MSch commented Mar 13, 2014

I think that's fixed by 8ca4d4f

@steveklabnik
Contributor

Closing as fixed.

@steveklabnik
Contributor

(Please let me know if it actually isn't)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment