Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store.createRecord + record.save do not resolve hasMany-Relationships #1497

Closed
sandreas opened this issue Nov 10, 2013 · 7 comments · Fixed by #2400
Closed

store.createRecord + record.save do not resolve hasMany-Relationships #1497

sandreas opened this issue Nov 10, 2013 · 7 comments · Fixed by #2400

Comments

@sandreas
Copy link

I tried to use store.createRecord for persisting hasMany-relationships, but they are not resolved in store.createRecord (store.push works correctly).

Here is a sample. Beware: There is no open REST api on jsbin, so requests are just "simulated". Open Firebug network protocol and look at "request payload":
http://jsbin.com/OJOJoFE/1/edit?html,js,output

Example code snippet:

App.Role = DS.Model.extend({
    name: DS.attr('string')
});
App.User = DS.Model.extend({
    username: DS.attr('string'),
    password: DS.attr('string'),
    roles: DS.hasMany('role')
});

    var model = {
      username: 'username',
      password: 'password',
      roles: [1,2]
    };

// create a record based on model data
record = this.get('store').createRecord('user', model);

// persist created record to REST 
// should send: username='username', password='password' and roles=[1,2]
// actually sends: username='username', password='password' and roles=[]
record.save();
@endash
Copy link
Contributor

endash commented Nov 11, 2013

Based on the jsbin and talking on IRC I would say not a bug. The list of properties passed to createRecord eventually gets passed to setProperties, and is a bit of a shortcut, thus it uses the hasMany setter, which doesn't do anything with the passed value, instead looking up ids from the data hash. push is how you would load raw data that you want to be transformed into relationships.

@sandreas
Copy link
Author

Well, i still would call this a bug (at least an important enhancement), because behavior is not consistent here... there is no way to create a record with relationships, without manually passing all the relationship-properties in...

// UserCreateController
actions: {
    saveFormValues: function (formValues) {
        var record = this.store.createRecord('user', formValues);
        // i think this is redundant here, if the roles are selected via the form, createRecord should take these as reference
        record.get('roles').addObjects(formValues.roles);
    }
}

@FeipingHunag
Copy link

App.ApplicationSerializer = DS.ActiveModelSerializer.extend
  serializeHasMany: (record, json, relationship) ->
    key = relationship.key
    kind = relationship.kind
    relationshipType = DS.RelationshipChange.determineRelationshipType(record.constructor, relationship)
    if relationshipType is 'manyToNone' or relationshipType is 'manyToMany'
      json[@keyForRelationship(key, kind)] = record.get(key).mapBy('id')

@igorT
Copy link
Member

igorT commented May 28, 2014

@FeipingHunag The problem here is not that the ids are not being serialized, but rather that createRecord doesn't accept relationships. I wouldn't expect it to accept ids in either case, but I feel like it should be possible to do

    var role1 = this.get('store').createRecord('role');
    var role2 = this.get('store').createRecord('role')

    var model = {
      username: 'username',
      password: 'password',
      roles: [role1, role2] 
    };

// create a record based on model data
record = this.get('store').createRecord('user', model);

@igorT igorT added the ssot label May 28, 2014
@sly7-7
Copy link
Contributor

sly7-7 commented Oct 14, 2014

@sandreas @igorT I assume this is fixed since ssot merge, isn't it ?

@igorT
Copy link
Member

igorT commented Oct 20, 2014

Not sure, let me test

@igorT
Copy link
Member

igorT commented Oct 20, 2014

Hasn't been fixed yet, but should be simple. We just need to make https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/store.js#L271 smarter @sly7-7 have time to try?

igorT added a commit that referenced this issue May 18, 2015
allowing to pass an has many relationship on create record. Fixes #1497
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants