{"action":"created","issue":{"url":"https://api.github.com/repos/nolanlawson/ember-pouch/issues/16","labels_url":"https://api.github.com/repos/nolanlawson/ember-pouch/issues/16/labels{/name}","comments_url":"https://api.github.com/repos/nolanlawson/ember-pouch/issues/16/comments","events_url":"https://api.github.com/repos/nolanlawson/ember-pouch/issues/16/events","html_url":"https://github.com/nolanlawson/ember-pouch/issues/16","id":49492936,"number":16,"title":"Bidirectional hasMany relationships are not loadable/saveable from the many side","user":{"login":"rsutphin","id":2503,"avatar_url":"https://avatars.githubusercontent.com/u/2503?v=3","gravatar_id":"","url":"https://api.github.com/users/rsutphin","html_url":"https://github.com/rsutphin","followers_url":"https://api.github.com/users/rsutphin/followers","following_url":"https://api.github.com/users/rsutphin/following{/other_user}","gists_url":"https://api.github.com/users/rsutphin/gists{/gist_id}","starred_url":"https://api.github.com/users/rsutphin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rsutphin/subscriptions","organizations_url":"https://api.github.com/users/rsutphin/orgs","repos_url":"https://api.github.com/users/rsutphin/repos","events_url":"https://api.github.com/users/rsutphin/events{/privacy}","received_events_url":"https://api.github.com/users/rsutphin/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":26,"created_at":"2014-11-20T04:54:06Z","updated_at":"2016-01-22T09:38:14Z","closed_at":null,"body":"The default behavior of `DS.JSONSerializer` is to not serialize the many side of bidirectional hasMany relationships. See emberjs/data#2494 for links to discussion about this.\r\n\r\nThe upshot for `ember-pouch` is that the contents of a bidirectional hasMany relationship isn't saved or loaded when working from the many side. E.g., say you have these models:\r\n\r\n```javascript\r\n// app/models/person.js\r\nexport default DS.Model.extend({\r\n  sandwiches: DS.hasMany('sandwich', { inverse: 'owner' })\r\n});\r\n\r\n// app/models/sandwich.js\r\nexport default DS.Model.extend({\r\n  owner: DS.belongsTo('person')\r\n});\r\n```\r\n\r\nIn this case you won't be able to load or save a person's sandwiches from the person. (You will be able to if you remove `owner` from sandwich, though.)\r\n\r\nThe workaround for this that other similar adapters use ([ember-localstorage-adapter][LS], [ember-indexeddb-adapter][IDB]) is to provide a serializer which forces these associations to be serialized on the hasMany side (as well as the belongsTo side). I'm doing this now in my own application to work around this issue.\r\n\r\n[LS]: https://github.com/kurko/ember-localstorage-adapter/blob/c32477626ce0e7c8447e1ee79eb78e64a8357d70/localstorage_adapter.js#L6\r\n[IDB]: https://github.com/kurko/ember-indexeddb-adapter/blob/0b1c73a1134606ccc9dfc1aadb86af5b4dd9fe4d/packages/indexeddb-adapter/lib/indexeddb_serializer.js#L2\r\n\r\nUnfortunately this requires copying a bunch of code from JSONSerializer (hence emberjs/data#2494). It also duplicates the information about the relationship, requiring both sides to be saved manually whenever the relationship changes.\r\n\r\nI'm not sure if there's a practical better solution. It appears that the ember-data solution is to have the server use different serializations for reads vs. writes, but that doesn't work for a local database. One idea I had was that the adapter could look up the IDs for the hasMany relationship dynamically — since all the data is local this might work, but it also might be complex to get right."},"comment":{"url":"https://api.github.com/repos/nolanlawson/ember-pouch/issues/comments/173861882","html_url":"https://github.com/nolanlawson/ember-pouch/issues/16#issuecomment-173861882","issue_url":"https://api.github.com/repos/nolanlawson/ember-pouch/issues/16","id":173861882,"user":{"login":"wooandoo","id":82373,"avatar_url":"https://avatars.githubusercontent.com/u/82373?v=3","gravatar_id":"","url":"https://api.github.com/users/wooandoo","html_url":"https://github.com/wooandoo","followers_url":"https://api.github.com/users/wooandoo/followers","following_url":"https://api.github.com/users/wooandoo/following{/other_user}","gists_url":"https://api.github.com/users/wooandoo/gists{/gist_id}","starred_url":"https://api.github.com/users/wooandoo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wooandoo/subscriptions","organizations_url":"https://api.github.com/users/wooandoo/orgs","repos_url":"https://api.github.com/users/wooandoo/repos","events_url":"https://api.github.com/users/wooandoo/events{/privacy}","received_events_url":"https://api.github.com/users/wooandoo/received_events","type":"User","site_admin":false},"created_at":"2016-01-22T09:38:14Z","updated_at":"2016-01-22T09:38:14Z","body":"@openhouse thanks for you simple solution!"}}