Skip to content

Commit ad5b7bd

Browse files
author
Yohan Robert
committed
[skip ci] Fix relationship link documentation
1 parent 93bbc59 commit ad5b7bd

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Fixes:
1212

1313
Misc:
1414

15+
- [#1981](https://github.com/rails-api/active_model_serializers/pull/1981) Fix relationship link documentation. (@groyoh)
16+
1517
### [v0.10.3 (2016-11-21)](https://github.com/rails-api/active_model_serializers/compare/v0.10.2...v0.10.3)
1618

1719
Fixes:

docs/howto/add_relationship_links.md

+23-21
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,42 @@ class Api::V1::UsersController < ApplicationController
1515
serializer: Api::V1::UserSerializer,
1616
include: []
1717
end
18+
end
1819
```
1920

2021
Bear in mind though that ActiveModelSerializers are [framework-agnostic](outside_controller_use.md), Rails is just a common example here.
2122

2223
### 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**
2425

2526
You can define an attribute in the resource, named `links`.
2627

2728
```ruby
2829
class Api::V1::UserSerializer < ActiveModel::Serializer
29-
attributes :id, :name, :links
30+
include Rails.application.routes.url_helpers
31+
32+
attributes :id, :name
3033

31-
def links
34+
attribute :links do
35+
id = object.id
3236
{
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)
3539
}
3640
end
3741
end
3842
```
3943

40-
This will resilt in (example is in jsonapi adapter):
44+
Using the `JSON` adapter, this will result in:
45+
4146
```json
4247
{
43-
"data": {
48+
{
4449
"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"
5254
}
5355
}
5456
}
@@ -58,7 +60,7 @@ This will resilt in (example is in jsonapi adapter):
5860
### Links as a property of the resource definiton
5961
**This is only applicable to JSONAPI adapter**
6062

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.
6264

6365
```ruby
6466
class Api::V1::UserSerializer < ActiveModel::Serializer
@@ -69,7 +71,7 @@ class Api::V1::UserSerializer < ActiveModel::Serializer
6971
end
7072
```
7173

72-
This will resilt in (example is in jsonapi adapter):
74+
This will result in (example is in jsonapi adapter):
7375
```json
7476
{
7577
"data": {
@@ -104,12 +106,12 @@ class Api::V1::UserSerializer < ActiveModel::Serializer
104106

105107
has_many :microposts, serializer: Api::V1::MicropostSerializer do
106108
link(:related) { api_v1_microposts_path(user_id: object.id) }
107-
end
108109

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
113115
end
114116
end
115117
```

0 commit comments

Comments
 (0)