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

Adding objects to a hasMany destroys other hasMany associations after save() #52

Closed
mattaddy opened this issue Apr 22, 2014 · 2 comments · Fixed by #54
Closed

Adding objects to a hasMany destroys other hasMany associations after save() #52

mattaddy opened this issue Apr 22, 2014 · 2 comments · Fixed by #54

Comments

@mattaddy
Copy link

Lets say I have the following application

var App = Ember.Application.create();

App.ApplicationAdapter = DS.FirebaseAdapter.extend({
  firebase: new Firebase('https://<my-app>.firebaseio.com')
});

App.User = DS.Model.extend({
  name: DS.attr('string'),
  posts: DS.hasMany('post', { async: true }),
  comments: DS.hasMany('comment', { async: true })
});

App.Post = DS.Model.extend({
  body: DS.attr('string')
});

App.Comment = DS.Model.extend({
  body: DS.attr('string')
});

And add a few posts to a given user (interacting with the Chrome console)

var store = App.__container__.lookup('store:main');

store.find('user', 1).then(function(u) {
  window.user = u
});

post1 = store.createRecord('post', { body: 'Post 1' });
post2 = store.createRecord('post', { body: 'Post 2' });
post3 = store.createRecord('post', { body: 'Post 3' });

user.get('posts').then(function(posts) {
  posts.addObjects([post1, post2, post3]);
  user.save().then(function() {
    post1.save();
    post2.save();
    post3.save();
  })
});

Creates the posts and associates them correctly in Firebase.

However, adding a comment to the user after the posts are saved results in the posts object being removed in Firebase

comment = store.createRecord('comment', { body: 'Comment 1' });

user.get('comments').then(function(comments) {
  comments.addObject(comment);
  user.save().then(function() { // this line destroys the user.posts object
    comment.save();
  })
});
@mattaddy
Copy link
Author

Before creating and associating the comment with the user, calling user.get('posts.length') returns 0.

jakejuby pushed a commit to jakejuby/emberFire that referenced this issue Apr 23, 2014
…before saving and comparing to the cache
@aputinski
Copy link
Collaborator

@mattaddy I'm working with @jakejuby to get a fix out for this soon.

aputinski added a commit that referenced this issue Apr 24, 2014
Closes #52 by loading a models hasMany relationships before saving
aputinski added a commit that referenced this issue Apr 24, 2014
* 'master' of github.com:firebase/emberFire:
  adding a test
  Closes #52 by loading a models hasMany relationships before saving and comparing to the cache
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.

2 participants