{"action":"created","issue":{"url":"https://api.github.com/repos/jmdobry/angular-data/issues/299","labels_url":"https://api.github.com/repos/jmdobry/angular-data/issues/299/labels{/name}","comments_url":"https://api.github.com/repos/jmdobry/angular-data/issues/299/comments","events_url":"https://api.github.com/repos/jmdobry/angular-data/issues/299/events","html_url":"https://github.com/jmdobry/angular-data/issues/299","id":55175463,"number":299,"title":"save()ing a parent that `has_many` children as nested resources.","user":{"login":"RickCarlino","id":1388608,"avatar_url":"https://avatars.githubusercontent.com/u/1388608?v=3","gravatar_id":"","url":"https://api.github.com/users/RickCarlino","html_url":"https://github.com/RickCarlino","followers_url":"https://api.github.com/users/RickCarlino/followers","following_url":"https://api.github.com/users/RickCarlino/following{/other_user}","gists_url":"https://api.github.com/users/RickCarlino/gists{/gist_id}","starred_url":"https://api.github.com/users/RickCarlino/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RickCarlino/subscriptions","organizations_url":"https://api.github.com/users/RickCarlino/orgs","repos_url":"https://api.github.com/users/RickCarlino/repos","events_url":"https://api.github.com/users/RickCarlino/events{/privacy}","received_events_url":"https://api.github.com/users/RickCarlino/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-01-22T16:36:23Z","updated_at":"2015-01-22T16:59:45Z","closed_at":null,"body":"I'm hitting circular references while trying to `save()` a resource that hasMany children (which are loaded into memory).\r\n```javascript\r\nDS.defineResource({\r\n  name: \"step\",\r\n  endpoint: 'steps',\r\n  baseUrl: '/api',\r\n  idAttribute: \"_id\",\r\n  relations: {\r\n    belongsTo: {\r\n      sequence: {\r\n        localKey: 'sequence_id',\r\n        localField: 'sequence',\r\n        parent: true\r\n      }\r\n    }\r\n  }\r\n});\r\n\r\nDS.defineResource({\r\n  name: \"sequence\",\r\n  endpoint: 'sequences',\r\n  baseUrl: '/api',\r\n  idAttribute: \"_id\",\r\n  relations: {\r\n    hasMany: {\r\n      step: {\r\n        localField: \"steps\",\r\n        foreignKey: \"sequence_id\"\r\n      }\r\n    }\r\n  }\r\n});\r\n\r\n// A sequence object that loaded fine.\r\nsomeSequence;\r\n\r\n// Pull all of the sequence.steps from the API via GET api/sequences/:id/steps\r\n// (works)\r\nDS.loadRelations('sequence', someSequence._id, ['step']);\r\n\r\n// The problem...\r\nDS.save('sequence', someSequence._id).catch(function(e) {\r\n  /* TypeError: Converting circular structure to JSON {stack: (...), message: \"Converting circular structure to JSON\"}message: \"Converting circular structure to JSON\"stack: (...) */\r\n});\r\n```\r\n\r\nAm I doing something wrong here? I could set `someSequence.steps` to `null` if need be, but I wanted to make sure there wasn't already a way of handling this within the library itself.\r\n\r\nThanks for the help and hard work you all put into maintaining this project."},"comment":{"url":"https://api.github.com/repos/jmdobry/angular-data/issues/comments/71054936","html_url":"https://github.com/jmdobry/angular-data/issues/299#issuecomment-71054936","issue_url":"https://api.github.com/repos/jmdobry/angular-data/issues/299","id":71054936,"user":{"login":"RickCarlino","id":1388608,"avatar_url":"https://avatars.githubusercontent.com/u/1388608?v=3","gravatar_id":"","url":"https://api.github.com/users/RickCarlino","html_url":"https://github.com/RickCarlino","followers_url":"https://api.github.com/users/RickCarlino/followers","following_url":"https://api.github.com/users/RickCarlino/following{/other_user}","gists_url":"https://api.github.com/users/RickCarlino/gists{/gist_id}","starred_url":"https://api.github.com/users/RickCarlino/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RickCarlino/subscriptions","organizations_url":"https://api.github.com/users/RickCarlino/orgs","repos_url":"https://api.github.com/users/RickCarlino/repos","events_url":"https://api.github.com/users/RickCarlino/events{/privacy}","received_events_url":"https://api.github.com/users/RickCarlino/received_events","type":"User","site_admin":false},"created_at":"2015-01-22T16:59:45Z","updated_at":"2015-01-22T16:59:45Z","body":"**UPDATE:** I found a cleaner, working solution with the code below, but am still curious if this the correct solution or if I am reinventing the wheel.\r\n\r\n```javascript\r\nDS.defineResource({\r\n  name: \"sequence\",\r\n  endpoint: 'sequences',\r\n  baseUrl: '/api',\r\n  idAttribute: \"_id\",\r\n  serialize: function(resourceName, data) {\r\n    var pojo;\r\n    pojo = _.clone(data);\r\n    delete pojo.steps;\r\n    return pojo;\r\n  },\r\n  relations: {\r\n    hasMany: {\r\n      step: {\r\n        localField: \"steps\",\r\n        foreignKey: \"sequence_id\"\r\n      }\r\n    }\r\n  }\r\n});\r\n```"}}