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 to hasMany (manyToMany) fixture associations not created #47

Open
benswinburne opened this issue Mar 9, 2020 · 1 comment
Open

Comments

@benswinburne
Copy link

This may not be a bug, but the example in the docs

If this happens to be a bi-directional relationship

  models: {
    user: Model.extend({
+     posts: hasMany()
    }),

    post: Model.extend({
      author: belongsTo("user"),
    }),
  },

then Mirage will add an array of foreign keys for the new hasMany association.

I have a many to many relationship

creative: Model.extend({
  campaigns: hasMany(),
}),

campaign: Model.extend({
  creatives: hasMany(),
}),

And some fixtures

  {
    id: '48dc4215-6f6c-470c-8c3e-40d95311ef19',
    name: 'Example',
    creativeIds: [
      '3fd34999-9519-42b0-8f08-1c2dfea8d338',
      '247c689a-8834-4b64-a052-b73228bdb407',
    ],
  },

When querying the campaigns, I can see the two creatives, but when querying the creatives, I can't see the campaigns. Is this expected behaviour or a bug?

@samselikoff
Copy link
Contributor

Sorry about the delayed response! I'm behind on issues a bit due to some consulting/training work.

In this case you would need to add the fixture data on both sides. So, you would need to add campaignIds to the creative fixtures.

Alternatively you could use seeds() hook and use the schema methods, in which case Mirage will take care of some of the bookkeeping for you:

seeds(server) {
  let c1 = server.create("creative", attrs);
  let c2 = server.create("creative", attrs);

  server.create("campaign", { name: "example", creatives: [c1, c2] });
}

or, if you really wanted to use hard-coded ids for some reason (or had them from somewhere else),

seeds(server) {
  let c1 = server.create("creative", {
    id: "3fd34999-9519-42b0-8f08-1c2dfea8d338",
    ...otherAttrs
  });
  let c2 = server.create("creative", {
    id: "247c689a-8834-4b64-a052-b73228bdb407",
    ...othetrAttrs
  });

  server.create("campaign", {
    name: "example",
    creativeIds: [
      "3fd34999-9519-42b0-8f08-1c2dfea8d338",
      "247c689a-8834-4b64-a052-b73228bdb407"
    ]
  });
}

@samselikoff samselikoff transferred this issue from miragejs/miragejs Mar 22, 2020
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