17

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"=>"fsfdsfdsfsd@fgfdgdfgf.de", "email"=>"dsfdsgds@frgdfgfgdfg.de", "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!

Dmitriy Khaykin
  • 5,238
  • 1
  • 20
  • 32
marcoow
  • 4,062
  • 1
  • 14
  • 21
  • I fixed it by changing the embedded config to DS.RESTAdapter.map('App.Account', { company: { embedded: 'load' } }); Not sure why that works actually... – marcoow Jul 02 '13 at 15:09
  • 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').createRecord – Cory Loken Jul 11 '13 at 17:11
  • 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: https://github.com/emberjs/data/pull/1067 – marcoow Jul 18 '13 at 17:38
  • 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 `/companies` – ThomasDurin Sep 05 '13 at 13:15
  • This might be a related question: http://stackoverflow.com/questions/20841947/custom-components-with-multiple-yield-like-sections/21590775#21590775 – alalani Feb 06 '14 at 22:50
  • Could you please answer this issue yourself, or somehow close it so it's no longer in "unanswered" in Stack Overflow? – Julian Leviston May 02 '14 at 04:48

2 Answers2

0

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?

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
0

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.

marcoow
  • 4,062
  • 1
  • 14
  • 21