mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Adds documentation for overriding default serializer based on conditions (#1730)
suggested changes update changelog
This commit is contained in:
committed by
L. Preston Sego III
parent
b75db81ca4
commit
ec15fa9de3
@@ -370,3 +370,19 @@ class PostSerializer < ActiveModel::Serializer
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
## Overriding association serializer lookup
|
||||
|
||||
If you want to define a specific serializer lookup for your associations, you can override
|
||||
the `ActiveModel::Serializer.serializer_for` method to return a serializer class based on defined conditions.
|
||||
|
||||
```ruby
|
||||
class MySerializer < ActiveModel::Serializer
|
||||
def self.serializer_for(model, options)
|
||||
return SparseAdminSerializer if model.class == 'Admin'
|
||||
super
|
||||
end
|
||||
|
||||
# the rest of the serializer
|
||||
end
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user