0
votes

My ember app is not sending my foreign key to the back-end.

I have a table called issues which is has a related table called categories

My model is:

import DS from 'ember-data';

export default DS.Model.extend({
    name:           DS.attr('string'),
    category_id:    DS.belongsTo('category'),
    description:    DS.attr('string')
});

My route is:

import Ember from 'ember';

export default Ember.Route.extend({
  model: function(){
    return this.store.findAll('issue');
  },
  actions: {
    create: function(){

        var issue = this.store.createRecord('issue');
        issue.name = this.get('controller').get('newName');
        issue.description = this.get('controller').get('newDescription');
        issue.category_id = parseInt(this.get('controller').get('newCategory'));
        //debugger;

        console.log(issue);
        issue.save();
    },
    ...
    other actions 
    ...
    }
  }
});

the console.log from above looks like the category_id is getting set correctly:

category_id: 3
description: "foobar"
name: "test"

However my JSON payload that gets sent to the backend looks like:

{"issue":{"name":"test","description":"foobar","category_id":null}}

I tried stepping through by adding a custom serialiser in app/serializers/application.js

export default DS.RESTSerializer.extend({
  ...

    serialize: function(snapshot,options){
      console.debug('options='+options);
      debugger;
      var json = this._super(snapshot, options);;

      return json;
    }
  ...
  });

But I got lost in all the super calling super indirection.

The snapshot.record has category_id: 3, but the json coming back from the this._super() call has category_id: null

options has includeID:true

Any clues will be much appreciated ...

Ember : 2.0.2
Ember Data : 2.0.0

1

1 Answers

0
votes

Your model definition is wrong, when dealing with relationships you define them just as you would define any other attribute, there is no need to use _id.

export default DS.Model.extend({
    name:           DS.attr('string'),
    category:    DS.belongsTo('category'),
    description:    DS.attr('string')
});

As for the creation you should always use setters/getters when dealing with ember objects:

create: function() {
  var issue = this.store.createRecord('issue', {
    name: this.get('controller').get('newName'),
    description: this.get('controller').get('newDescription'),
    category: this.get('controller').get('newCategory') // assuming new category is a DS.Model instance of category
  });
  issue.save();
}

If you wish to stick to the syntax you have you would use issue.set('name', this.get('controller').get('newName')), from the looks of your code it seems you are going about this in the wrong way.

You should have a this.route('new') nested under your issues route, that way you wouldn't have to use the controller to store information.

You would simply set the model of the new route to:

model: function() {
  return this.store.createRecord('issue');
}

Your template would make use of the input helpers like so:

{{input value=model.name}} and your action would just get the currentModel and call .save().