Your main problem is that you are trying to add Jobs by ID. You never created a task object, although not to mention setting your identifier! You have created only companies.
The best way to add tasks is the addJob() function, which you give to the company (or company) identifier, and it creates the Job model for you.
Full code as per your example
and in particular:
var Person = Backbone.RelationalModel.extend({ relations: [{ type: 'HasMany', key: 'jobs', relatedModel: Job, reverseRelation: { key: 'person' //includeInJSON: false //if you don't want to show person } }], addJob: function (company) { this.get('jobs').add(new Job({ company: company })); } }); paul.addJob(1); paul.addJob(2);
Works great. You might also want to set includeInJSON to false for your inverse relationship to the job, to exclude the person!
[{ "company": { "id": 1, "name": "ibm", "employees": [null] }, "person": { "name": "Paul", "jobs": [null, { "company": { "id": 2, "name": "apple", "employees": [null] } }] } }, { "company": { "id": 2, "name": "apple", "employees": [null] }, "person": { "name": "Paul", "jobs": [{ "company": { "id": 1, "name": "ibm", "employees": [null] } }, null] } }]
source share