17
votes

I have the following models:

App.Company = DS.Model.extend({
  name:  DS.attr('string'),
  accounts: DS.hasMany('App.Account', {
    inverse: 'company'
  })
});

App.Account = DS.Model.extend({
  login:                 DS.attr('string'),
  first_name:            DS.attr('string'),
  last_name:             DS.attr('string'),
  email:                 DS.attr('string'),
  password:              DS.attr('string'),
  password_confirmation: DS.attr('string'),
  company:               DS.belongsTo('App.Company')
});

The company is defined as being embedded in the account:

DS.RESTAdapter.map('App.Account', {
  company: { embedded: 'always' }
});

When I create a new account, the company data is correctly embedded in the account data and I'm seeing the POST request that I expect on the server side:

Started POST "/accounts" for 127.0.0.1 at 2013-06-27 13:30:53 +0200
Processing by AdminUsersController#create as JSON
  Parameters: {"account"=>{"login"=>"fsdfdf", "first_name"=>"fgdfgh", "last_name"=>"[email protected]", "email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "company"=>{"name"=>"gfdhgtrhzrh"}}}

However, I'm also seeing an additional POST request for the company itself:

Started POST "/companies" for 127.0.0.1 at 2013-06-27 13:30:53 +0200
Processing by CompaniesController#create as JSON
  Parameters: {"company"=>{"name"=>"gfdhgtrhzrh"}}

I'm setting up the models as follows:

this.transaction = this.get('store').transaction();
var account = this.transaction.createRecord(App.Account, {});
account.set('company', this.transaction.createRecord(App.Company, {}));

When the user clicks save, I simply commit the transaction:

this.transaction.commit();

Is that a bug or am I doing something wrong? Spent quite some time on that already...

Thanks for help!

2
I fixed it by changing the embedded config to DS.RESTAdapter.map('App.Account', { company: { embedded: 'load' } }); Not sure why that works actually...marcoow
I think this is a with belongsTo as hasMany behaves as expected. With has many (even with no children) you actually use the relationship to create a child record, but with belongsTo the relationship is null so you can't create it with this.get('company').createRecordCory Loken
I created a pull request which I think fixes it - I don't see why it shouldn't work fir belongsTo just as well as it does for hasMany: github.com/emberjs/data/pull/1067marcoow
If you want to avoid this unwanted POST request on /companies, you have to create your new Company record into another transaction. This way when you will commit the transaction of you Account record, you wont have this POST request on /companiesThomasDurin
This might be a related question: stackoverflow.com/questions/20841947/…alalani

2 Answers

0
votes

this.transaction.createRecord(App.Company, {})

The code fragment creates the separate company entity. Is it really such a surprise there is a post action for it?

0
votes

As far as I remember that was never actually supported in the (old) version of Ember Data I used back then. Newer versions handle that case differently anyway so I'd say this is outdated and close it.