|
| 1 | +[Back to Guides](../README.md) |
| 2 | + |
| 3 | +# How to add relationship links |
| 4 | + |
| 5 | +ActiveModelSerializers offers you many ways to add links in your JSON, depending on your needs. |
| 6 | +The most common use case for links is supporting nested resources. |
| 7 | + |
| 8 | +The following examples are without included relationship data (`include` param is empty), |
| 9 | +specifically the following Rails controller was used for these examples: |
| 10 | + |
| 11 | +```ruby |
| 12 | +class Api::V1::UsersController < ApplicationController |
| 13 | + def show |
| 14 | + render jsonapi: User.find(params[:id]), |
| 15 | + serializer: Api::V1::UserSerializer, |
| 16 | + include: [] |
| 17 | + end |
| 18 | +``` |
| 19 | + |
| 20 | +Bear in mind though that ActiveModelSerializers are [framework-agnostic](outside_controller_use.md), Rails is just a common example here. |
| 21 | + |
| 22 | +### Links as an attribute of a resource |
| 23 | +**This is applicable to JSONAPI, JSON and Attributes adapters** |
| 24 | + |
| 25 | +You can define an attribute in the resource, named `links`. |
| 26 | + |
| 27 | +```ruby |
| 28 | +class Api::V1::UserSerializer < ActiveModel::Serializer |
| 29 | + attributes :id, :name, :links |
| 30 | + |
| 31 | + def links |
| 32 | + { |
| 33 | + self: api_v1_user_path(object.id), |
| 34 | + microposts: api_v1_microposts_path(user_id: object.id) |
| 35 | + } |
| 36 | + end |
| 37 | +end |
| 38 | +``` |
| 39 | + |
| 40 | +This will resilt in (example is in jsonapi adapter): |
| 41 | +```json |
| 42 | +{ |
| 43 | + "data": { |
| 44 | + "id": "1", |
| 45 | + "type": "users", |
| 46 | + "attributes": { |
| 47 | + "name": "Example User", |
| 48 | + "links": { |
| 49 | + "self": "/api/v1/users/1", |
| 50 | + "microposts": "/api/v1/microposts?user_id=1" |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | + |
| 58 | +### Links as a property of the resource definiton |
| 59 | +**This is only applicable to JSONAPI adapter** |
| 60 | + |
| 61 | +You can use the `links` class method to define the links you need in the resource's primary data. |
| 62 | + |
| 63 | +```ruby |
| 64 | +class Api::V1::UserSerializer < ActiveModel::Serializer |
| 65 | + attributes :id, :name |
| 66 | + |
| 67 | + link(:self) { api_v1_user_path(object.id) } |
| 68 | + link(:microposts) { api_v1_microposts_path(user_id: object.id) } |
| 69 | +end |
| 70 | +``` |
| 71 | + |
| 72 | +This will resilt in (example is in jsonapi adapter): |
| 73 | +```json |
| 74 | +{ |
| 75 | + "data": { |
| 76 | + "id": "1", |
| 77 | + "type": "users", |
| 78 | + "attributes": { |
| 79 | + "name": "Example User" |
| 80 | + }, |
| 81 | + "links": { |
| 82 | + "self": "/api/v1/users/1", |
| 83 | + "microposts": "/api/v1/microposts?user_id=1" |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### Links that follow the JSONAPI spec |
| 90 | +**This is only applicable to JSONAPI adapter** |
| 91 | + |
| 92 | +If you have a JSONAPI-strict client that you are working with (like `ember-data`) |
| 93 | +you need to construct the links inside the relationships. Also the link to fetch the |
| 94 | +relationship data must be under the `related` attribute, whereas to manipulate the |
| 95 | +relationship (in case of many-to-many relationship) must be under the `self` attribute. |
| 96 | + |
| 97 | +You can find more info in the [spec](http://jsonapi.org/format/#document-resource-object-relationships). |
| 98 | + |
| 99 | +Here is how you can do this: |
| 100 | + |
| 101 | +```ruby |
| 102 | +class Api::V1::UserSerializer < ActiveModel::Serializer |
| 103 | + attributes :id, :name |
| 104 | + |
| 105 | + has_many :microposts, serializer: Api::V1::MicropostSerializer do |
| 106 | + link(:related) { api_v1_microposts_path(user_id: object.id) } |
| 107 | + end |
| 108 | + |
| 109 | + #this is needed to avoid n+1, gem core devs are working to remove this necessity |
| 110 | + #more on: https://github.com/rails-api/active_model_serializers/issues/1325 |
| 111 | + def microposts |
| 112 | + object.microposts.loaded ? object.microposts : object.microposts.none |
| 113 | + end |
| 114 | +end |
| 115 | +``` |
| 116 | + |
| 117 | +This will result in: |
| 118 | + |
| 119 | +```json |
| 120 | +{ |
| 121 | + "data": { |
| 122 | + "id": "1", |
| 123 | + "type": "users", |
| 124 | + "attributes": { |
| 125 | + "name": "Example User" |
| 126 | + }, |
| 127 | + "relationships": { |
| 128 | + "microposts": { |
| 129 | + "data": [], |
| 130 | + "links": { |
| 131 | + "related": "/api/v1/microposts?user_id=1" |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | +``` |
0 commit comments