<br />
<b>Warning</b>:  The magic method SFML_Singleton::__wakeup() must have public visibility in <b>/home/public/wp-content/plugins/sf-move-login/inc/classes/class-sfml-singleton.php</b> on line <b>72</b><br />
{"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":"<p>The <a href=\"https:\/\/ember-twiddle.com\/104407460799f1c16c4c0dc88daf975a\">many-to-many example<\/a>\u00a0provided with the <a href=\"http:\/\/www.ember-cli-mirage.com\/blog\/2017\/01\/09\/0-3-0-beta-series\/\">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<p>Using that example as a very useful starting point, I updated it to <a href=\"https:\/\/ember-twiddle.com\/1df2b1a7d5e8cd1004dbf454feed453a\">include factories and random joins<\/a> between the two models. The interesting code happens in the <a href=\"https:\/\/gist.github.com\/mikeu\/1df2b1a7d5e8cd1004dbf454feed453a#file-mirage-scenarios-default-js\">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<p>Given a function <span class=\"lang:default decode:true crayon-inline \">randomSubset<\/span>\u00a0\u00a0to select a random subset of the given array, the fundamental setup is straightforward:<\/p>\n<pre class=\"lang:js mark:7-8 decode:true\" title=\"Joining many authors to each of many books\">let authors = server.createList('author', 6)\r\nlet books = server.createList('book', 5)\r\n  \r\nbooks.forEach((book) =&gt; {\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<p>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\u00a0<span class=\"lang:default decode:true crayon-inline \">book.authorIds<\/span>\u00a0to that array of integers instead.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8230; <span class=\"more\"><a class=\"more-link\" href=\"https:\/\/www.munderwood.ca\/index.php\/2017\/03\/27\/many-to-many-relationships-in-ember-cli-mirage-factories\/\">[Read more&#8230;]<\/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}]}}