Posting to nested resource url


#1

I have a simple Rails app (https://github.com/hqmq/comic_collector) which has nested resources:

resources :books do
  resources :issues
end

I have set up two Ember models for these resources and am trying to create a new issue. Here are my models (using ember-tools):

var Book = DS.Model.extend({
  name:            DS.attr('string'),
  cover_image_url: DS.attr('string'),
  created_at:      DS.attr('date'),
  updated_at:      DS.attr('date'),

  issues:          DS.hasMany('App.Issue', { embedded: true }),

  totalIssues: function() {
    return this.get('issues').get('length');
  }.property('issues.@each'),

  totalPrice: function() {
    return this.get('issues').getEach('price').reduce(function(accum, price) {
      return (Math.round(accum * 100) +  Math.round(price * 100)) / 100;
    })
  }.property('issues.@each')
});

module.exports = Book;

var Issue = DS.Model.extend({
  number:          DS.attr('number'),
  title:           DS.attr('string'),
  cover_image_url: DS.attr('string'),
  price:           DS.attr('number'),
  created_at:      DS.attr('date'),
  updated_at:      DS.attr('date'),

  book:            DS.belongsTo('App.Book')
});

module.exports = Issue;

Here’s the controller which handles the post of a new issue:

var BookController = Ember.ObjectController.extend({
  createIssue: function() {
    var data = {
      book_id: this.get('model.id'),
      cover_image_url: this.get('new_cover_image_url'), 
      number: this.get('new_number'),
      price: this.get('new_price'),
      title: this.get('new_title'),
    }
    var issues = this.get('model').get('issues');
    var newIssue = issues.createRecord(data);
    this.get('store').commit();
  }
});

module.exports = BookController;

When the createIssue function is called, it posts to /issues instead of /books/1/issues.

Can you please help? I’m sure the solution is easy, I’m just not seeing anything in the docs or blog posts.

Eric


#2

I think the solution (right now anyway) is to not use nested resources on the server. Give your client a full data-store API and move on :slight_smile:


#3

Is avoiding nested resources server-side still the best way to go? Reads from the server seem to work flawlessly as long as links: {related: ...}} is set, but there doesn’t seem to be anywhere to tell ember data that it should create new sub-resources by posting them as a subresource.

I could just give up on trying to use nested resources, but I don’t want to have a bunch of individual queries to the backend to load data that could have been fetched with a couple of queries. Should I just give up on trying to nest resources and focus on finding a way to side-load related data instead?

As a side-note I’m using MongoDB as a backing store and if I have to keep everything flat that means I’ll have to keep a separate collection for the child resource. If I could guarantee that I’ll always have the parent resource’s ID available on the server side I could keep parent and child in the same document, which means I can shard on the parent’s ID and still always side-load all data relevant to the parent resource.

What are other people doing?

– Allan