Warning: The magic method SFML_Singleton::__wakeup() must have public visibility in /home/public/wp-content/plugins/sf-move-login/inc/classes/class-sfml-singleton.php on line 72

Warning: Cannot modify header information - headers already sent by (output started at /home/public/wp-content/plugins/sf-move-login/inc/classes/class-sfml-singleton.php:72) in /home/public/wp-includes/rest-api/class-wp-rest-server.php on line 1642

Warning: Cannot modify header information - headers already sent by (output started at /home/public/wp-content/plugins/sf-move-login/inc/classes/class-sfml-singleton.php:72) in /home/public/wp-includes/rest-api/class-wp-rest-server.php on line 1642

Warning: Cannot modify header information - headers already sent by (output started at /home/public/wp-content/plugins/sf-move-login/inc/classes/class-sfml-singleton.php:72) in /home/public/wp-includes/rest-api/class-wp-rest-server.php on line 1642

Warning: Cannot modify header information - headers already sent by (output started at /home/public/wp-content/plugins/sf-move-login/inc/classes/class-sfml-singleton.php:72) in /home/public/wp-includes/rest-api/class-wp-rest-server.php on line 1642

Warning: Cannot modify header information - headers already sent by (output started at /home/public/wp-content/plugins/sf-move-login/inc/classes/class-sfml-singleton.php:72) in /home/public/wp-includes/rest-api/class-wp-rest-server.php on line 1642

Warning: Cannot modify header information - headers already sent by (output started at /home/public/wp-content/plugins/sf-move-login/inc/classes/class-sfml-singleton.php:72) in /home/public/wp-includes/rest-api/class-wp-rest-server.php on line 1642

Warning: Cannot modify header information - headers already sent by (output started at /home/public/wp-content/plugins/sf-move-login/inc/classes/class-sfml-singleton.php:72) in /home/public/wp-includes/rest-api/class-wp-rest-server.php on line 1642

Warning: Cannot modify header information - headers already sent by (output started at /home/public/wp-content/plugins/sf-move-login/inc/classes/class-sfml-singleton.php:72) in /home/public/wp-includes/rest-api/class-wp-rest-server.php on line 1642
{"id":187,"date":"2017-03-27T10:50:46","date_gmt":"2017-03-27T16:50:46","guid":{"rendered":"http:\/\/www.munderwood.ca\/?p=187"},"modified":"2017-03-27T11:30:48","modified_gmt":"2017-03-27T17:30:48","slug":"many-to-many-relationships-in-ember-cli-mirage-factories","status":"publish","type":"post","link":"https:\/\/www.munderwood.ca\/index.php\/2017\/03\/27\/many-to-many-relationships-in-ember-cli-mirage-factories\/","title":{"rendered":"Many-to-many relationships in Ember CLI Mirage factories"},"content":{"rendered":"

The many-to-many example<\/a>\u00a0provided with the introduction of Mirage 0.3<\/a>\u00a0lays out how to create many-to-many joins between specific instances of two models. It does this by hard coding the creation of a few books and a couple of authors, as well as the links between them, but stops short of using factories to generate such relationships on demand.<\/p>\n

Using that example as a very useful starting point, I updated it to include factories and random joins<\/a> between the two models. The interesting code happens in the default scenario<\/a>, where two independent sets are generated\u2015one of authors, one of books\u2015and then each book is randomly assigned between one and three of the authors. (A side effect of this approach is that it is possible to end up with authors on the list that have zero books associated with them.)<\/p>\n

Given a function randomSubset<\/span>\u00a0\u00a0to select a random subset of the given array, the fundamental setup is straightforward:<\/p>\n

let authors = server.createList('author', 6)\r\nlet books = server.createList('book', 5)\r\n  \r\nbooks.forEach((book) => {\r\n  let numAuthors = faker.random.number({min: 1, max: 3})\r\n  let authorsOfBook = randomSubset(authors, numAuthors)\r\n  book.authors = authorsOfBook\r\n  book.save()\r\n});<\/pre>\n

Alternatively, if instead of using the author model instances you have a random subset of the author ids, the same result can be achieved simply by setting\u00a0book.authorIds<\/span>\u00a0to that array of integers instead.<\/p>\n

 <\/p>\n","protected":false},"excerpt":{"rendered":"

The many-to-many example\u00a0provided with the introduction of Mirage 0.3\u00a0lays out how to create many-to-many joins between specific instances of two models. It does this by hard coding the creation of a few books and a couple of authors, as well as the links between them, but stops short of using factories to generate such relationships … [Read more…]<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[9],"_links":{"self":[{"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/posts\/187"}],"collection":[{"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/comments?post=187"}],"version-history":[{"count":2,"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/posts\/187\/revisions"}],"predecessor-version":[{"id":189,"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/posts\/187\/revisions\/189"}],"wp:attachment":[{"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/media?parent=187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/categories?post=187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.munderwood.ca\/index.php\/wp-json\/wp\/v2\/tags?post=187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}