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

Create record from an association directly. #115

Closed
wants to merge 7 commits into from

Conversation

juggy
Copy link
Contributor

@juggy juggy commented Feb 23, 2012

Hello,

I spoke about this with @wycats this morning. With this pull request you can directly create a new record directly on the association. So instead of:

var myRecord = store.createRecord({...});
otherRecord.get("association").pushObject(myRecord);

You can do:

otherRecord.get("association").create({...})

It will use the parentRecord store to create the record.

This somewhat reflects Rails functionality in regards to relations and gives a clear direction to the user as to how to use them.

Thanks,
Julien.

@tchak
Copy link
Member

tchak commented Feb 23, 2012

Shouldn't it be called createRecord ?

@wyaeld
Copy link

wyaeld commented Feb 23, 2012

It is possible / desirable to have it also update the other end on a bi-directional relationship? I've noticed this seems to be lacking in ember right now. eg.

test("updates both sides of a hasMany association", function() {
  var Tag = DS.Model.extend({
    name: DS.attr('string')
    person: DS.belongsTo('Person')
  });

  var Person = DS.Model.extend({
    name: DS.attr('string'),
    tags: DS.hasMany(Tag)
  });

  var store = DS.Store.create();
  store.load(Person, 1, { id: 1, name: "Tom Dale"});

  var person = store.find(Person, 1);
  person.get("tags").create({name:"cool"}, store);

  equal(get(person, 'tags').objectAt(0).get("name"), "cool", "tag values are passed along");
  equal(get(person, 'tags').objectAt(0).get("person"), person, "belongsTo end of the association has been set");

@juggy
Copy link
Contributor Author

juggy commented Feb 24, 2012

@tchak Yes it should, I updated it.

@wyaeld It should definitely do keep track of the different associations and I think it should be the default behaviour (for both hasMany and belongsTo, both ways). It would affect the whole framework. I am not sure I should bundle it in this PR.

@wycats
Copy link
Member

wycats commented Feb 26, 2012

@juggy @wyaeld we are currently working on making bidirectional relationships work. In fact, the code to make it work is git stashed on the Tomhuda pairing machine as we speak. I think we will ship it soon.

@juggy it seems like new records should be in the same transaction as their parent record. Can you update this code to ensure that?

@wycats wycats mentioned this pull request Feb 26, 2012
@leepfrog
Copy link

+1 on this pull.. would save a lot of grief for me

@tomdale
Copy link
Member

tomdale commented Apr 26, 2012

Hey @juggy, would you mind making this rebase cleanly so we can merge it in? Thanks amigo.

Conflicts:
	packages/ember-data/lib/system/model_array/many_array.js
	packages/ember-data/tests/unit/associations_test.js
@juggy
Copy link
Contributor Author

juggy commented Apr 27, 2012

Use #230

@juggy juggy closed this Apr 27, 2012
@runspired runspired added 🏷️ feat This PR introduces a new feature and removed Improvement labels Sep 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ feat This PR introduces a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants