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

Can't save parent record in a hasMany/belongsTo relationship #1750

Closed
stuporglue opened this issue Feb 14, 2014 · 6 comments · Fixed by #2526
Closed

Can't save parent record in a hasMany/belongsTo relationship #1750

stuporglue opened this issue Feb 14, 2014 · 6 comments · Fixed by #2526

Comments

@stuporglue
Copy link

When I have objects with a hasMany <--> belongsTo relationship I can't save the parent object. Saving causes it to lose track of its child objects.

I am using the Fixture adapter.

I have tried several things, you can see them here: http://stuporglue.github.io/saving_ember_data/nodirty.html

The objects are as follows:

            // We're using ember-data...
            MF.Store = DS.Store.extend({
                adapter: DS.FixtureAdapter
            });

            // Two objects, a parent...
            MF.Parent = DS.Model.extend({
                name: DS.attr(),
                childs: DS.hasMany('child')
            });
            MF.Parent.FIXTURES = []; // ...with fixtures

            // ...and a child
            MF.Child = DS.Model.extend({
                name: DS.attr(),
                parent: DS.belongsTo('parent')
            });
            MF.Child.FIXTURES = [];

When I do any of the following, the Parent forgets about its children:

  • Set Child's Parent. Save the Child. Save the Parent
  • Set Child's Parent. Save the Parent.
  • Add Child to Parent. Save Parent.
  • Add Child to Parent. Save Child. Save Parent.
@stuporglue
Copy link
Author

To see what happens when you click a button, open the JavaScript console.

@knownasilya
Copy link
Contributor

@stuporglue what version of ED are you using?

@stuporglue
Copy link
Author

I'm using v1.0.0-beta.5.

I think it's related to the discussion here which links to this pull request.

When I modify serializeHasMany to also check for relationshipType === 'manyToOne' my test cases start working.

@knownasilya
Copy link
Contributor

You should probably be defining the adapter like so:

MF.ApplicationAdapter = DS.FixtureAdapter;

The store no longer needs to be defined, at least I'm pretty sure 😄

@GetContented
Copy link

This problem has been plaguging me for a while, too, and others:
http://discuss.emberjs.com/t/saving-multiple-hasmany/4907/3

@danlshields
Copy link

This appears to be caused by JSONSerializer's serializeHasMany function not serializing manyToOne relationships: 7f752ad.

Would the best choice of action here be to implement a custom serializer for the FixtureAdapter?

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.

4 participants