KDani
KDani

Reputation: 458

How should I embed 2 models with a many-to-many relationship?

I have two models with a many-to-many relationship:

So each blog has a tags property, and each tag has a blogs property. I also have two API endpoints: /blogs and /tags

My problem is, if I include and embed the models in the serializer, I got a circular reference with error message "Maximum call stack size exceeded"

Mirage REPL

I'd like to see something like this:

GET /api/blogs
[
  {
    "id": "1",
    "title": "Some title",
    "author": "Author 0",
    "tags": [
      {
        "id": "1",
        "name": "Tag 0"
      },
      {
        "id": "2",
        "name": "Tag 1"
      }
    ]
  }
]

GET /api/tags
[
  {
    "id": "1",
    "name": "Tag 0",
    "blogs": [
      {
        "id": "1",
        "title": "Some title",
        "author": "Author 0"
      }
    ]
  },
  {
    "id": "2",
    "name": "Tag 1",
    "blogs": [
      {
        "id": "1",
        "title": "Some title",
        "author": "Author 0"
      }
    ]
  }
]

Upvotes: 1

Views: 64

Answers (0)

Related Questions