Add support for nested REST resources #186

heeton opened this Issue Apr 5, 2012 · 41 comments


None yet
heeton commented Apr 5, 2012

I've seen a couple of discussions about this, and it seems no satisfactory outcome yet?

We've put together a little change to the standard RESTAdapter, that supports custom URLs. I'd like some opinions on it: If you like it, I'll update the tests and RESTAdapter and put in a pull request.

So, you can now define a custom resource URL in your model (I don't think this is the right place, but I couldn't think of a better way).

App.Post = DS.Model.extend({
  title: DS.attr('string'),
  user: DS.belongsTo('App.User'),
  url: (function(){
      return '/users/' + this.getPath('') + '/posts';

And the RESTAdapter checks for this custom URL.

DS.NestedRESTAdapteri = DS.RESTAdapter.extend({
  createRecord: function(store, type, model) {

    var data = {};
    data[root] = model.toJSON();

    // ---------------- NEW CODE -----------------------
      var url = model.get('url');
    } else {
      var root = this.rootForType(type);
      var url = this.buildURL(root);
    // -------------------------------------------------
    this.ajax(url, "POST", {
      data: data,
      success: function(json) {
        this.sideload(store, type, json, root);
        store.didCreateRecord(model, json[root]);

NOTE: We're only using this for our CREATE action, so it'll need some tweaking to work for all actions. I'm happy to do all that if this has a chance of getting merged.

boy-jer commented May 11, 2012

+1 for this but i will suggest you look at how you can make your work an enhancement of pull request #57: which specified a namespace for the REST adaptor which is added to all the ajax URLs. Also see pull request #39, which does something nearer to the enhancement you are proposing but was abandoned in favour of #57 which unfortunately didn't go all the way to address the issue you are trying to fix.


App.Comment = DS.Model.extend({
  post: DS.belongsTo('App.Post', {nested: true})

Note the nested option.

I have a commit at twalpole@239ad99 that implements @justinko suggested method of specifying the nested hierarchy and produces the correct nested urls for create, update, delete (along with tests)- It does not yet work for the read since the location of where the fetch comes from is lost going through the store. I'm trying to figure out the best way to handle the read/fetch portion, if anyone has any suggestions I'd be more than happy to implement.

@twalpole 👍 👏

boy-jer commented May 16, 2012

👏 good progress @twalpole

ok - I've added in read support too and submitted it as pull request #257 . Theres probably some refactoring that could be done to clean it up a little bit but I want to get some other eyes on it to see what I missed.

boy-jer commented May 17, 2012

👏 thanks for all your work @twalpole. Pls @justinko and @heeton kindly register your interest or add your votes to the pull request, so that @tomdale and @wycats could see that others want the feature beside me and @twalpole.



ijonas commented Jun 5, 2012


wagenet commented Jun 21, 2012

@boy-jer For what it's worth, "voting" doesn't really hold any value in these discussions.

boy-jer commented Jun 23, 2012

@wagenet, in principle i agree with you but for what it's worth, having large enough people indicate their interest could make you, @tomdale , @wycats and any other key person like @ebryn to add your views and say yes or no in one fell swoop and everyone can move on, so that was the whole essence of the voting call.



niklas commented Sep 16, 2012


Support to create a hasOne would be awesome, too. For example: with Torrent.hasOne('transfer') the invokation of torrent.createTransfer() should result in a POST to /torrents/23/transfer.


Where's the bottleneck or the problem? This issue is open since almost a year now, is there any news, @heeton, @twalpole or @wagenet?

Most APIs I've seen are nested, like /articles/3813/comments, it feels a bit limited to fly with /articles/3813?comments=true...

toranb commented Mar 7, 2013

As a workaround, my pull request below adds the ability to create a nested url (using the parent record)


So in your example ...


would return all the posts for user with a pk of 1

It would still require a custom adapter as the default RESTAdatper does not support this (the very reason for this pull request as I understand it). But it has worked for me -my django adapter does this today as the default REST backend requires hasMany lookups to use this approach instead of what you get out of the box w/ ember-data

Nice! I'll have a look at it...

samstr commented May 5, 2013

Any progress here?

Would be great to have nested REST resources. Any progress?

delijah commented May 23, 2013






twalpole commented Jul 2, 2013

To the people that keep upvoting this, I had put an updated version as PR 790 - but the powers that be decided it was not the approach they wanted to follow.

Then, IMHO, a clear and official statement somewhere in the guides would be helpful.

+1 on the clear & official statement request

heeton commented Jul 2, 2013

Sorry for starting and abandoning!
I've not been active with Ember for a few months now, so I'm going to gracefully bow out and let some more informed people argue for me :)

bugant commented Jul 3, 2013


andyhot commented Jul 9, 2013


bugant commented Jul 19, 2013

@sidonath PR looks promising. Is there any chance it will get merged?

irae commented Aug 3, 2013


atonse commented Aug 9, 2013


wycats commented Sep 3, 2013

The findHasMany adapter hook can be used to add support for this, but we should have a nicer way to opt into this exact scenario and provide the needed URL.

I'll keep it in mind going forward.

@wycats wycats closed this Sep 3, 2013
cwiese commented May 31, 2014

This is not solved yet!?!?!?!? So many reason to stop using Ember - after weeks of development around nested Routes and now EmberData ignores the nesting! I have yet to see how this is better than Backbone or Angular

cwiese commented May 31, 2014

That is funny. After reading dozens of blogs and github issues, I am starting to believe that the Nesting is only a client side routing mechanism and the REST API should not used nesting (there could be 100's of possible combinations of nesting for a relational model). So I will change the server.

atsjj commented Jun 6, 2014

@cwiese FWIW, I have a large Ember project that uses a nested API that goes several layers deep. This is accomplished in our JSON with each returned parent object containing a links hash, with the related model's typekey as the key and value as a URL.

I haven't had any problems with this structuring and it works well enough for what we're doing. We structure our JSON close to the JSONAPI format with the REST adapter. We are looking at switching to the JSONAPI adapter/serializer for ember-data.

cwiese commented Jun 6, 2014

Great info - I did see that solution. Thanks

@blimmer blimmer referenced this issue in emberjs/rfcs Oct 1, 2014

RFC: Ember Data url templates #4

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