Skip to content

Commit 8eef7a0

Browse files
committed
Adds README documentation for relationship links
1 parent 89f007d commit 8eef7a0

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,36 @@ class MovieSerializer
245245
end
246246
```
247247

248+
#### Links on a Relationship
249+
250+
You can specify [relationship links](http://jsonapi.org/format/#document-resource-object-relationships) by using the `links:` option on the serializer. Relationship links in JSON API are useful if you want to load a parent document and then load associated documents later due to size constraints (see [related resource links](http://jsonapi.org/format/#document-resource-object-related-resource-links))
251+
252+
```ruby
253+
class MovieSerializer
254+
include FastJsonapi::ObjectSerializer
255+
256+
has_many :actors, links: {
257+
self: :url,
258+
related: -> (object) {
259+
"https://movies.com/#{object.id}/actors"
260+
}
261+
}
262+
end
263+
```
264+
265+
This will create a `self` reference for the relationship, and a `related` link for loading the actors relationship later. NB: This will not automatically disable including the data in the relationship, you'll need to do that using the yielded block:
266+
267+
```ruby
268+
has_many :actors, links: {
269+
self: :url,
270+
related: -> (object) {
271+
"https://movies.com/#{object.id}/actors"
272+
}
273+
} do |movie|
274+
movie.actors.limit(5)
275+
end
276+
```
277+
248278
### Compound Document
249279

250280
Support for top-level and nested included associations through ` options[:include] `.

0 commit comments

Comments
 (0)