Skip to content

Commit afe3c93

Browse files
committed
Add more association documentation (#1635)
[DOC] Add more association documentation
1 parent d2917fd commit afe3c93

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

docs/general/serializers.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,26 @@ Serialization of the resource `title`
3838

3939
### Associations
4040

41+
The interface for associations is, generically:
42+
43+
> `association_type(association_name, options, &block)`
44+
45+
Where:
46+
47+
- `association_type` may be `has_one`, `has_many`, `belongs_to`.
48+
- `association_name` is a method name the serializer calls.
49+
- optional: `options` may be:
50+
- `key:` The name used for the serialized association.
51+
- `serializer:`
52+
- `if:`
53+
- `unless:`
54+
- `virtual_value:`
55+
- optional: `&block` is a context that returns the association's attributes.
56+
- prevents `association_name` method from being called.
57+
- return value of block is used as the association value.
58+
- yields the `serializer` to the block.
59+
- `include_data false` prevents the `data` key from being rendered in the JSON API relationship.
60+
4161
#### ::has_one
4262

4363
e.g.
@@ -58,6 +78,14 @@ def cached_blog
5878
end
5979
```
6080

81+
``ruby
82+
has_one :blog, if: :show_blog?
83+
84+
def show_blog?
85+
scope.admin?
86+
end
87+
```
88+
6189
#### ::has_many
6290
6391
e.g.

0 commit comments

Comments
 (0)