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

hasMany relationship not updating from the belongsTo side. #116

Closed
dweremeichik opened this issue Mar 3, 2016 · 10 comments
Closed

hasMany relationship not updating from the belongsTo side. #116

dweremeichik opened this issue Mar 3, 2016 · 10 comments

Comments

@dweremeichik
Copy link
Contributor

It appears that the hasMany relationships are not updating when a belongsTo record is created.
The two models i am using are:

export default Model.extend({
  name: DS.attr('string', {defaultValue: ""}),
  valueCents: DS.attr('number', {defaultValue: 0}),
  type: DS.attr('string', {defaultValue: ""}),
  discounts: DS.hasMany('discount')
});

and

export default Model.extend({
  triggerQuantity: DS.attr('number'),
  percentage: DS.attr('number'),
  item: DS.belongsTo('item')
});

I am attempting to add a discount to an existing item with the following code:

addDiscount(item){
      let discount = item.store.createRecord('discount',{
        item: item,
        triggerQuantity: 3,
        percentage: 3
      });
      discount.save();
    }

According to the ember docs, this should "just work". In the database, I can see that discount record has the id of the item that it was attached to. However the item record just contains an empty array, instead of an array with a reference to the discount record.

I am using:
Ember 2.4.1
ember-data 2.4.0
ember-pouch 3.1.1

@backspace
Copy link
Collaborator

You have to save the parent record as well. This isn’t typical for Ember Data, but it’s required in this case to save both ends of the relationship.

The documentation should be updated to reflect this. But people expecting behaviour like with other Ember Data adapters will not expect it.

(For background on this, see #16)

@dweremeichik
Copy link
Contributor Author

Thanks for pointing me towards that. However it seems that even if I do that, it still leaves the array empty on the item side. I tried an approach similar to what you did in your initializer (mine in a component).

https://github.com/backspace/ember-relational-pouch-example/blob/master/app/initializers/data-populator.js

@backspace
Copy link
Collaborator

Are you saving the item after having saved the discount?

That code you linked to is pretty old, but I have this more recent acceptance test that constructs related objects.

With the code you have in your first comment, you could do something like this at the end of addDiscount: discount.save().then(() => item.save());

@dweremeichik
Copy link
Contributor Author

Sorry for the lack of code. My computer is running offline for the moment. I did exactly as you mentioned and it is still not working. I'm beginning to wonder if there is an issue with my models or the way i set up ember pouch. The strange part is that the discount is getting the item id.

@dweremeichik
Copy link
Contributor Author

Because this should be working, i will start a project from scratch tomorrow to test the functionality out and make sure it is isolated to this... I'll use the same ~version that you used in your adventure project, and rip off your ember pouch configs to make sure i didn't do something dumb.

@dweremeichik
Copy link
Contributor Author

Okay, so I started from scratch, not using the same version as you, however 2.4 is supposed to be minor fixes and backwards compatible. Our adapters are almost identical (I added debugging to mine), and our config/environment files are similar in regards to ember-pouch. It appears that it is still not working, so this disproves my configs being bad theory. As you can see here, my approach is pretty spot on to what you mentioned earlier. As before, the discount is getting the ID, but no matter how I save it (order wise), the Item won't update it's reference to the discount.

@dweremeichik
Copy link
Contributor Author

Just an update, I swapped ember-pouch out with emberfire (firebase) and the way I'm writing it seems to work there. I tried troubleshooting ember-pouch earlier, but my knowledge of both emberjs and ember-pouch are limited. I would like to investigate further, but it may be a while if at all before I get back to it. Unfortunately at the moment I need to get a prototype out.

@backspace
Copy link
Collaborator

I figured out what’s missing! Your application doesn’t implement the ember-pouch serialiser. If you add this file at app/serializers/application.js, the parent contains references to the IDs of its children:

import { Serializer } from 'ember-pouch';

export default Serializer.extend();

I’ll look at adding this to the documentation and/or addon default imports.

backspace added a commit that referenced this issue Mar 5, 2016
Including the serialiser by default would preclude people
having to define it themselves and avoid confusion such
as in #116. But it could be installed manually with a
generator, as well.
@dweremeichik
Copy link
Contributor Author

Thank you, that did it! I owe you a beer or something!

I'm looking into adding to the documentation regarding saving both the child and parent for relationships, not sure where the best place for it would be though. I'm thinking probably before the "EmberPouch Blueprints" section. I will mull it over tonight, and create a pull request in the morning. I might open an issue for discussion on the documentation regarding organization and ordering.

@dweremeichik
Copy link
Contributor Author

Issue solved, and documentation created on its behalf #119. Blueprint in progress #124.

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

No branches or pull requests

2 participants