lundi 26 septembre 2016

Saving a belongsTo relationship fails

I'm having trouble saving a new record because the belongsTo relationship isn't being assigned to the newly created record (promise something Promise).

Anyway, my backend is rails and these are the params that are required:

Parameters: {"photo"=>{"title"=>"Hello word", "image"=>"someimage.jpg", "user_id"=>nil}}

As you can see the user_id is nil even though my session successfully returns the logged in user id

Ember Controller

  let photo = this.store.createRecord('photo', {
    title: this.get("newModel.title"),
    image: this.get("newModel.image")
  });

  this.get('store').findRecord('user', this.get('session.data.authenticated.userId')).then(function(user){
    photo.set('user', user);
  });

  photo.save();

This fails because from reading the ember docs you cannot assign a promise as a relationship (?!?!?). There are no errors returned from ember side, however the belongs_to relationship for the user looks like this

user <DS.PromiseObject:ember891>

I also seem to notice that Ember keeps adding the user_id param despite no value being present

Note that I cannot save the record without user_id because of presence validation on the backend and it's not a solution to remove those.

Any solutions for this?




Aucun commentaire:

Enregistrer un commentaire