@@ -15,40 +15,42 @@ class Api::V1::UsersController < ApplicationController
15
15
serializer: Api ::V1 ::UserSerializer ,
16
16
include: []
17
17
end
18
+ end
18
19
```
19
20
20
21
Bear in mind though that ActiveModelSerializers are [ framework-agnostic] ( outside_controller_use.md ) , Rails is just a common example here.
21
22
22
23
### Links as an attribute of a resource
23
- ** This is applicable to JSONAPI, JSON and Attributes adapters**
24
+ ** This is applicable to JSON and Attributes adapters**
24
25
25
26
You can define an attribute in the resource, named ` links ` .
26
27
27
28
``` ruby
28
29
class Api ::V1 ::UserSerializer < ActiveModel ::Serializer
29
- attributes :id , :name , :links
30
+ include Rails .application.routes.url_helpers
31
+
32
+ attributes :id , :name
30
33
31
- def links
34
+ attribute :links do
35
+ id = object.id
32
36
{
33
- self: api_v1_user_path(object. id),
34
- microposts: api_v1_microposts_path(user_id: object. id)
37
+ self: api_v1_user_path(id),
38
+ microposts: api_v1_microposts_path(user_id: id)
35
39
}
36
40
end
37
41
end
38
42
```
39
43
40
- This will resilt in (example is in jsonapi adapter):
44
+ Using the ` JSON ` adapter, this will result in:
45
+
41
46
``` json
42
47
{
43
- "data" : {
48
+ {
44
49
"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
- }
50
+ "name" : " John" ,
51
+ "links" : {
52
+ "self" : " /api/v1/users/1" ,
53
+ "microposts" : " /api/v1/microposts?user_id=1"
52
54
}
53
55
}
54
56
}
@@ -58,7 +60,7 @@ This will resilt in (example is in jsonapi adapter):
58
60
### Links as a property of the resource definiton
59
61
** This is only applicable to JSONAPI adapter**
60
62
61
- You can use the ` links ` class method to define the links you need in the resource's primary data.
63
+ You can use the ` link ` class method to define the links you need in the resource's primary data.
62
64
63
65
``` ruby
64
66
class Api ::V1 ::UserSerializer < ActiveModel ::Serializer
@@ -69,7 +71,7 @@ class Api::V1::UserSerializer < ActiveModel::Serializer
69
71
end
70
72
```
71
73
72
- This will resilt in (example is in jsonapi adapter):
74
+ This will result in (example is in jsonapi adapter):
73
75
``` json
74
76
{
75
77
"data" : {
@@ -104,12 +106,12 @@ class Api::V1::UserSerializer < ActiveModel::Serializer
104
106
105
107
has_many :microposts , serializer: Api ::V1 ::MicropostSerializer do
106
108
link(:related ) { api_v1_microposts_path(user_id: object.id) }
107
- end
108
109
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
110
+ microposts = object.microposts
111
+ # The following code is needed to avoid n+1 queries.
112
+ # Core devs are working to remove this necessity.
113
+ # See: https://github.com/rails-api/active_model_serializers/issues/1325
114
+ microposts.loaded? ? microposts : microposts.none
113
115
end
114
116
end
115
117
```
0 commit comments