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

resource links on a polymorphic belongs_to association #81

Closed
wants to merge 1 commit into from

Conversation

camallen
Copy link
Contributor

I wanted to start a discussion around how to handle a polymorphic belongs_to relation on a resource.

Just returning the FK id doesn't contain the polymorphic context of the linked resource, resulting in an ambiguous link. I've implemented a simple fix that fits my needs as my polymorphic association target overrides the to_param method however if the association target doesn't then this problem still remains.

Perhaps there is a better way of embedding the association type in the links hash? Thoughts?

@mhuggins
Copy link

There isn't anything in the JSON API format about polymorphic relationships, and I think that's because it's outside the scope of the format. There are ways of handling this in the response structure itself though. Instead of referring to the polymorphic name you're likely using in your Ruby code, you should instead refer to the actual class it is cast to. That also means you'll need to use the legitimate class names when defining "links" or "linked" relationships.

For example, instead of this:

{
  "groups": [
    {
      "id": "1",
      "name": "Ruby Fans",
      "links": {
        "users": ["1", "3", "9"]
      }
    }
  ],
  "links": {
    "groups.users": [
      {"id": "1", ...},
      {"id": "3", ...},
      {"id": "9", ...}
    ]
  }
}

...you'd instead need to force it to output something like this:

{
  "groups": [
    {
      "id": "1",
      "name": "Ruby Fans",
      "links": {
        "readers": ["1", "3"],
        "admins": ["9"],
      }
    }
  ],
  "links": {
    "groups.readers": [
      {"id": "1", ...},
      {"id": "3", ...}
    ],
    "groups.admins": [
      {"id": "9", ...}
    ]
  }
}

@camallen
Copy link
Contributor Author

It seems the JSON API spec has changed recently and they now allow the link to represented as a resource object with a type param, e.g:

{
  "id": "1",
  "title": "Rails is Omakase",
  "links": {
    "author": {
      "href": "http://example.com/people/17",
      "id": "17",
      "type": "people"
    }
  }
}

The spec now states NOTE: In case of conflict, an individual resource object's links object will take precedence over a top-level links object. This would allow the links resource representation to be authoritative.

I think Restpack Serializer could now reflect upon the association and if it's polymorphic it could output a resource object representing the association.

I'll try and implement some code and update some specs to achieve this.

@camallen
Copy link
Contributor Author

closing in favour of #122

@camallen camallen closed this Feb 12, 2015
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

Successfully merging this pull request may close these issues.

None yet

2 participants