You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+29-6Lines changed: 29 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -221,7 +221,7 @@ end
221
221
```
222
222
223
223
### Links Per Object
224
-
Links are defined in FastJsonapi using the `link` method. By default, link are read directly from the model property of the same name. In this example, `public_url` is expected to be a property of the object being serialized.
224
+
Links are defined in FastJsonapi using the `link` method. By default, links are read directly from the model property of the same name. In this example, `public_url` is expected to be a property of the object being serialized.
225
225
226
226
You can configure the method to use on the object for example a link with key `self` will get set to the value returned by a method called `url` on the movie object.
227
227
@@ -245,15 +245,38 @@ class MovieSerializer
245
245
end
246
246
```
247
247
248
-
### Meta Per Resource
249
-
250
-
For every resource in the collection, you can include a meta object containing non-standard meta-information about a resource that can not be represented as an attribute or relationship.
248
+
#### Links on a Relationship
251
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))
252
251
253
252
```ruby
254
253
classMovieSerializer
255
254
includeFastJsonapi::ObjectSerializer
256
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 loading the data in the relationship, you'll need to do that using the `lazy_load_data` option:
266
+
267
+
```ruby
268
+
has_many :actors, lazy_load_data:true, links: {
269
+
self::url,
270
+
related:-> (object) {
271
+
"https://movies.com/#{object.id}/actors"
272
+
}
273
+
}
274
+
```
275
+
276
+
### Meta Per Resource
277
+
278
+
For every resource in the collection, you can include a meta object containing non-standard meta-information about a resource that can not be represented as an attribute or relationship.
set_id | ID of Object | ```set_id :owner_id ``` or ```set_id { |record| "#{record.name.downcase}-#{record.id}" }```
431
454
cache_options | Hash to enable caching and set cache length | ```cache_options enabled: true, cache_length: 12.hours, race_condition_ttl: 10.seconds```
432
-
id_method_name | Set custom method name to get ID of an object | ```has_many :locations, id_method_name: :place_ids ```
455
+
id_method_name | Set custom method name to get ID of an object (If block is provided for the relationship, `id_method_name` is invoked on the return value of the block instead of the resource object) | ```has_many :locations, id_method_name: :place_ids ```
433
456
object_method_name | Set custom method name to get related objects | ```has_many :locations, object_method_name: :places ```
434
457
record_type | Set custom Object Type for a relationship | ```belongs_to :owner, record_type: :user```
435
458
serializer | Set custom Serializer for a relationship | ```has_many :actors, serializer: :custom_actor``` or ```has_many :actors, serializer: MyApp::Api::V1::ActorSerializer```
0 commit comments