2
2
3
3
# How to add relationship links
4
4
5
- AMS offers you many ways to add links in your JSON, depending on your needs.
5
+ ActiveModelSerializers offers you many ways to add links in your JSON, depending on your needs.
6
+ Usually the most common use case for links is supporting nested resources.
6
7
7
8
The following examples are in Rails but the logic is the same for any framework
8
9
used.
9
10
10
- The following examples preassume that you don't want to load any data for relationships (include param is empty),
11
+ The following examples assume that you don't want to load any data for relationships (include param is empty),
11
12
specifically the following Rails controller was used for those examples:
12
13
13
14
``` ruby
14
- class Api ::V1 ::UsersController
15
+ class Api ::V1 ::UsersController < ApplicationController
15
16
def show
16
17
render jsonapi: User .find(params[:id ]),
17
18
serializer: Api ::V1 ::UserSerializer ,
@@ -26,7 +27,7 @@ You can just define an attribute in the resource, named `links` and return the
26
27
links you want there. An example:
27
28
28
29
``` ruby
29
- class Api ::V1 ::UserSerializer
30
+ class Api ::V1 ::UserSerializer < ActiveModel :: Serializer
30
31
attributes :id , :name , :links
31
32
32
33
def links
@@ -56,21 +57,21 @@ This will resilt in (example is in jsonapi adapter):
56
57
```
57
58
58
59
59
- ### Links as an property of the resource definiton
60
+ ### Links as a property of the resource definiton
60
61
** This is only applicable to JSONAPI adapter**
61
62
62
63
You can use the ` links ` helper method to define the links you need in the resource definition level.
63
64
64
65
``` ruby
65
- class Api ::V1 ::UserSerializer
66
+ class Api ::V1 ::UserSerializer < ActiveModel :: Serializer
66
67
attributes :id , :name
67
68
68
- link(:self ) {api_v1_user_path(object.id)}
69
- link(:microposts ) {api_v1_microposts_path(user_id: object.id)}
69
+ link(:self ) { api_v1_user_path(object.id) }
70
+ link(:microposts ) { api_v1_microposts_path(user_id: object.id) }
70
71
end
71
72
```
72
73
73
- This will resilt in (example is in jsonapi adapter):
74
+ This will resilt in (example is in jsonapi adapter):
74
75
``` json
75
76
{
76
77
"data" : {
@@ -91,17 +92,23 @@ This will resilt in (example is in jsonapi adapter):
91
92
** This is only applicable to JSONAPI adapter**
92
93
93
94
If you have a JSONAPI-strict client that you are working with (like ` ember-data ` )
94
- you need to struct the links inside the relationships. Here is how:
95
+ you need to struct the links inside the relationships. Also the link to fetch the
96
+ relationship data must be under the ` related ` attribute, whereas to manipulate the
97
+ relationship (in case of many-to-many relationship) must be under the ` self ` attribute.
98
+
99
+ You can find more info in the [ spec] ( http://jsonapi.org/format/#document-resource-object-relationships ) .
100
+
101
+ Here is how you can do this:
95
102
96
103
``` ruby
97
- class Api ::V1 ::UserSerializer
104
+ class Api ::V1 ::UserSerializer < ActiveModel :: Serializer
98
105
attributes :id , :name
99
106
100
107
has_many :microposts , serializer: Api ::V1 ::MicropostSerializer do
101
- link(:microposts ) {api_v1_microposts_path(user_id: object.id)}
108
+ link(:related ) { api_v1_microposts_path(user_id: object.id) }
102
109
end
103
110
104
- # this is needed to avoid n+1, AMS core devs are working to remove this necessity
111
+ # this is needed to avoid n+1, gem core devs are working to remove this necessity
105
112
# more on: https://github.com/rails-api/active_model_serializers/issues/1325
106
113
def microposts
107
114
object.microposts.loaded ? object.microposts : object.microposts.none
@@ -123,7 +130,7 @@ This will result in:
123
130
"microposts" : {
124
131
"data" : [],
125
132
"links" : {
126
- "microposts " : " //example.com/blogs/relationships/posts "
133
+ "related " : " /api/v1/microposts?user_id=1 "
127
134
}
128
135
}
129
136
}
0 commit comments