mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Add more association documentation (#1635)
[DOC] Add more association documentation
This commit is contained in:
parent
d2917fd606
commit
afe3c93807
@ -38,6 +38,26 @@ Serialization of the resource `title`
|
|||||||
|
|
||||||
### Associations
|
### Associations
|
||||||
|
|
||||||
|
The interface for associations is, generically:
|
||||||
|
|
||||||
|
> `association_type(association_name, options, &block)`
|
||||||
|
|
||||||
|
Where:
|
||||||
|
|
||||||
|
- `association_type` may be `has_one`, `has_many`, `belongs_to`.
|
||||||
|
- `association_name` is a method name the serializer calls.
|
||||||
|
- optional: `options` may be:
|
||||||
|
- `key:` The name used for the serialized association.
|
||||||
|
- `serializer:`
|
||||||
|
- `if:`
|
||||||
|
- `unless:`
|
||||||
|
- `virtual_value:`
|
||||||
|
- optional: `&block` is a context that returns the association's attributes.
|
||||||
|
- prevents `association_name` method from being called.
|
||||||
|
- return value of block is used as the association value.
|
||||||
|
- yields the `serializer` to the block.
|
||||||
|
- `include_data false` prevents the `data` key from being rendered in the JSON API relationship.
|
||||||
|
|
||||||
#### ::has_one
|
#### ::has_one
|
||||||
|
|
||||||
e.g.
|
e.g.
|
||||||
@ -58,6 +78,14 @@ def cached_blog
|
|||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
``ruby
|
||||||
|
has_one :blog, if: :show_blog?
|
||||||
|
|
||||||
|
def show_blog?
|
||||||
|
scope.admin?
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
#### ::has_many
|
#### ::has_many
|
||||||
|
|
||||||
e.g.
|
e.g.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user