Skip to content

Commit 8223a25

Browse files
committed
Grammar fixing
1 parent ca5455c commit 8223a25

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

docs/howto/add_relationship_links.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22

33
# How to add relationship links
44

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

78
The following examples are in Rails but the logic is the same for any framework
89
used.
910

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),
1112
specifically the following Rails controller was used for those examples:
1213

1314
```ruby
14-
class Api::V1::UsersController
15+
class Api::V1::UsersController < ApplicationController
1516
def show
1617
render jsonapi: User.find(params[:id]),
1718
serializer: Api::V1::UserSerializer,
@@ -26,7 +27,7 @@ You can just define an attribute in the resource, named `links` and return the
2627
links you want there. An example:
2728

2829
```ruby
29-
class Api::V1::UserSerializer
30+
class Api::V1::UserSerializer < ActiveModel::Serializer
3031
attributes :id, :name, :links
3132

3233
def links
@@ -56,21 +57,21 @@ This will resilt in (example is in jsonapi adapter):
5657
```
5758

5859

59-
### Links as an property of the resource definiton
60+
### Links as a property of the resource definiton
6061
**This is only applicable to JSONAPI adapter**
6162

6263
You can use the `links` helper method to define the links you need in the resource definition level.
6364

6465
```ruby
65-
class Api::V1::UserSerializer
66+
class Api::V1::UserSerializer < ActiveModel::Serializer
6667
attributes :id, :name
6768

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) }
7071
end
7172
```
7273

73-
This will resilt in (example is in jsonapi adapter):
74+
This will resilt in (example is in jsonapi adapter):
7475
```json
7576
{
7677
"data": {
@@ -91,17 +92,23 @@ This will resilt in (example is in jsonapi adapter):
9192
**This is only applicable to JSONAPI adapter**
9293

9394
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:
95102

96103
```ruby
97-
class Api::V1::UserSerializer
104+
class Api::V1::UserSerializer < ActiveModel::Serializer
98105
attributes :id, :name
99106

100107
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) }
102109
end
103110

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
105112
#more on: https://github.com/rails-api/active_model_serializers/issues/1325
106113
def microposts
107114
object.microposts.loaded ? object.microposts : object.microposts.none
@@ -123,7 +130,7 @@ This will result in:
123130
"microposts": {
124131
"data": [],
125132
"links": {
126-
"microposts": "//example.com/blogs/relationships/posts"
133+
"related": "/api/v1/microposts?user_id=1"
127134
}
128135
}
129136
}

0 commit comments

Comments
 (0)