question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Document how to model a many-to-many relationship

See original GitHub issue

Currently, you must explicitly model the join model.

Eventually, we will add a has_and_belongs_to_many, similar to Rails.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
samselikoffcommented, Jan 4, 2017

@morhook thank you. With the 3.x beta series underway this should much simpler very soon. You can follow along at https://github.com/samselikoff/ember-cli-mirage/pull/965

2reactions
scottfiskcommented, Mar 10, 2016

So I understand the “explicitly create a join model” but do not understand how I make this join model correctly translate to a 2 sided hasMany in ember data. Perhaps that is the sort of thing you are talking about documenting? Maybe something in the serializer? Below is the recommended setup I have gathered from the issues. Mirage models:

// mirage/models/post.js
export default Model.extend({
  postTags: hasMany()
});
// mirage/models/tag.js
export default Model.extend({
  postTags: hasMany()
});
// mirage/models/post-tag.js
export default Model.extend({
  post: belongsTo(),
  tag: belongsTo()
});

And some fixtures:

// mirage/fixtures/posts.js
export default [
  {
    id: 1
    title: "Post 1"
  }
];
// mirage/fixtures/tags.js
export default [
  {
    id: 1,
    text: "Tag 1"
  },
  {
    id: 2,
    text: "Tag 2"
  }
];
// mirage/fixtures/post-tags.js
export default [
  {
    postId: 1,
    tagId: 1
  },
  {
    postId: 1,
    tagId: 2
  }
];

With the ember data models being:

//app/models/post.js
export default Model.extend({
  tags: hasMany('tag', { async: true })
});
//app/models/tag.js
export default Model.extend({
  posts: hasMany('post', { async: true })
});

After reading through the issues the one thing I am missing is the best practice using mirage to wire up the tags and posts async hasMany relations so it works with the ember data setup. From the declarations above I wouldn’t expect posts or tags to work in ember data so how do I go about that?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Many-to-many relationships - Django documentation
To define a many-to-many relationship, use ManyToManyField . In this example, an Article can be published in multiple Publication objects, and a Publication ......
Read more >
MongoDB Many-to-Many Association - Stack Overflow
User - Create, Read, Update, Delete (CRUD operations like a free-standing entity). This can be modeled as the following document templates: User: {...
Read more >
Many-to-Many Relationship in MongoDB - Model Intricate Data
This article shows you how to design many-to-many relationship in MongoDB ... MongoDB documentation writes only of how to model one-to-many ...
Read more >
MongoDB Many-to-Many Relationship with Mongoose examples
Model Many-to-Many Relationships in MongoDB ... data models; Use Mongoose Model functions to create Documents; Populate referenced documents.
Read more >
3. How to model many to many relationships? - Books by Agiliq
3. How to model many to many relationships?¶ ... A many-to-many relationship refers to a relationship between tables in a database when a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found