Ember Data 1.0Beta - Rails / ActiveModelSerializer compatability


#1

When trying out the newest version of Ember data it seems that relations should now be represented as models: ["1", "2", "3"] instead of model_ids: ["1", "2", "3"]. I’ve monkey-patched ActiveModelSerializer to fit with this new style.

When the records are saved back to the server, obviously I get the records without the _ids which conflicts with what ActiveRecord is expecting. So I was ready to write a special deserializer for my Rails controllers.

However, I read this old AMS pull request and I saw that the jj-abrams website documentation still uses the model_ids style (maybe because they just haven’t been updated yet).

Does anyone have some insight into the direction of this API in the near-future? I’m wondering if Ember Data will be sticking with the current relationship serialization style, heading for something more like jsonapi.org with the nested links styax, or going back to the current ASM style with _id and _ids. Also does anyone have this worked out with ASM, Rails, and the new Ember Data in a way they like?


#2

I don’t know if this work but if it does then you won’t need to monkey-patch ActiveModelSerializers, but in the documentation, somewhere under embedded association, https://github.com/rails-api/active_model_serializers#embedding-associations

It says you can also specify a different root for the embedded objects than the key used to reference them and though their example is about embedded the whole associated objects rather than their id but it might work.

    class PostSerializer < ActiveModel::Serializer
       embed :ids, include: true
       attributes :id, :title, :body
       has_many :comments, key: :comment_ids, root: :comment_objects
   end

#3

If you don’t want to monkey patch ASM you can just set the key property on each relationship:

class PostSerializer < ActiveModel::Serializer
   embed :ids
   attributes :id
   has_many :comments, key: :comments
end

Is that what you’re getting at? I’m not sure what setting the root as comment_objects is for.


#4

Yes that is what I was getting at. But it seems a new version of ActiveModelSerializer is being written that would be compatible with ember-data 1.0 beta and jsonapi.org based on the discussion here: https://github.com/rails-api/active_model_serializers/issues/346#issuecomment-22631323


#5

Looks like we’ve got the same reading list :slight_smile:

I’ve been reading all that info on ASM and looking over the recent issues on Ember Data. It seems like both ASM and Ember Data have a soft commitment to eventually support the JSON API spec… at some point. I’m currently weighing my options of either getting Ember Data to use the _ids style again or trying to get a JSON API style going on both ends.


#6

According to https://github.com/emberjs/data/blob/master/TRANSITION.md#rest-adapter-and-serializer-configuration:

beta.1 expects comments key now instead of comments_ids. This is likely to be configurable in beta.2.


#7

@mitchlloyd Someone is trying to get the Ember Data to use the _ids:

Basic DS.ActiveModelSerializer implementation


#8

Thanks for the heads up. Nice to see that the golden path for Rails integration is being paved.


#9

I’ve been trying to adjust my API so that it uses “links” because the number of records I was getting to was causing 414 errors (request URL too large or 404 because of malformed request - chopped off bits of id specifiers).

Only trouble is, it seems to be missing data when I do it this way. I was wondering if that’s coz I’m specifying the links incorrectly in my serializer.

Seems that if I take off the links from my serializer, things start sort of working again… but I need it because of the long requests.

I’m specifying it like this:

    def links
      {
        "child_blocks" => child_blocks_designer_block_url(object.id, format: :json) # "/blocks/#{object.id}/child_blocks"
      }
    end

Does that seem right?

I’m using the latest ember & ember data (1.0beta7)…